Home  >  Article  >  Web Front-end  >  Get several extensions and abbreviations of DOM objects_javascript skills

Get several extensions and abbreviations of DOM objects_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:25:271130browse

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);
};

Statement:
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