Today, I saw a script on the Internet to obtain elements based on classname, which is recorded here for future use.
CopyCode Code: var getelementsbyclassname = function (searchclass, node, tag ){
If (document. getelementsbyclassname ){
Return document. getelementsbyclassname (searchclass)
} Else {
Node = node | document;
Tag = tag | "*";
VaR classes = searchclass. Split (""),
Elements = (TAG = "*" & node. All )? Node. ALL: node. getelementsbytagname (TAG ),
Patterns = [],
Returnelements = [],
Current,
Match;
VaR I = classes. length;
While (-- I> = 0 ){
Patterns. Push (New Regexp ("(^ | \ s)" + classes [I] + "(\ s | $ )"));
}
VaR J = elements. length;
While (-- j> = 0 ){
Current = elements [J];
Match = false;
For (var k = 0, KL = patterns. length; k <kl; k ++ ){
Match = patterns [K]. Test (current. classname );
If (! Match) break;
}
If (MATCH) returnelements. Push (current );
}
Return returnelements;
}
}
The following are some other introductions on the Internet.
Getelementsbyclassname in Dom is explained as follows: Dom APIs provide three methods for retrieving elements (getelementbyid, getelementsbyname, getelementsbytagname). People who frequently write CSS will naturally have doubts, is there any method for retrieving elements based on style class names? Unfortunately, this method is not available in dom1/2. prototype has extended the DOM method very early and added getelementsbyclassname, from the method name perspective, it seems very orthodox. Like the names of the first three methods, the code is analyzed, but it is still implemented by getelementsbytagname. This method is not elegant, because we need to traverse all elements, check whether the element contains the name of the target style class, and return an array of elements that meet the conditions. Google did not find a more elegant and efficient alternative.Copy codeThe Code is as follows: function getelementsbyclassname (classname, parentelement ){
VaR elems = ($ (parentelement) | document. Body). getelementsbytagname ("*");
VaR result = [];
For (I = 0; j = elems [I]; I ++ ){
If ("" + J. classname + ""). indexof ("" + classname + "")! =-1 ){
Result. Push (j );
}
}
Return result;
}
getelementsbyattribute (for example, getelementsbyvalue, getelementsbystyle, and getelementsbytype) copy Code the code is as follows: document. getelementsbyclassname = function (classname, obox) {
// applicable to obtaining all HTML elements containing a specific classname in 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;
}< BR >}< br> return elements;
}
Document. getelementsbytype = function (stypevalue, obox ){
// Obtain all HTML elements of a specific type in an HTML block, such as input, script, and link.
This. d = obox | document;
VaR children = This. D. getelementsbytagname ('*') | document. All;
VaR elements = new array ();
For (var ii = 0; II <children. length; II ++ ){
If (Children [II]. type = stypevalue ){
Elements. Push (Children [II]);
}
}
Return elements;
}
Function $ (){
VaR elements = new array ();
For (var ii = 0; II <arguments. length; 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) {// The name method can only be used for the entire document and cannot be used for a limited range.
Return document. getelementsbyname (s );
};