Home > Web Front-end > JS Tutorial > body text

Get several extensions and abbreviations of DOM objects_javascript skills

WBOY
Release: 2016-05-16 19:25:27
Original
1129 people have browsed it

Referring to the idea of ​​getElementsByClassName in prototype.js, several methods of obtaining objects that can be frequently used in DEOM operations have been extended, making it more convenient and accurate to obtain objects:
document.getElementsByClassName = function(className, oBox) {
//Suitable for getting all HTML elements containing a specific className within an HTML block
this.d= oBox || document;
var children = this.d.getElementsByTagName( '*') || document.all;
var elements = new Array();
for (var ii = 0; ii var child = children[ii] ;
var classNames = child.className.split(' ');
for (var j = 0; j if (classNames[j] == className) {
elements.push(child);
break;
}
}
}
return elements;
}

document.getElementsByType = function(sTypeValue ,oBox) {
//Suitable for obtaining all HTML elements belonging to a specific type within an HTML block, such as: input, script, link, etc.
this.d= oBox || document;
var children = this.d.getElementsByTagName('*') || document.all;
var elements = new Array();
for (var ii = 0; ii if (children[ii].type == sTypeValue) {
elements.push(children[ii]);
}
}
return elements;
}

function $() {
var elements = new Array();
for (var ii = 0; ii var element = arguments[ii] ;
if (typeof element == 'string')
element = document.getElementById(element);
if (arguments.length == 1)
return element;
elements.push (element);
}
return elements;
}

$Cls = function (s,o){
return document.getElementsByClassName(s,o);
};

$Type = function (s,o){
return document.getElementsByType(s,o);
};

$Tag = function (s,o ){
this.d=o || document;
return this.d.getElementsByTagName(s);
};

$Name = function (s){ //By name The method can only be used for the entire document and cannot be limited to its scope
return document.getElementsByName(s);
};

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!