IE does not support getelementsbyclassname solutions

Source: Internet
Author: User
Tags split

You typically use getElementsByTagName ("*") to remove all elements of a document, and then iterate through it, using regular expressions to find matching elements to return to an array. Because IE5 does not support document.getElementsByTagName ("*"), use branch document.all to prevent errors

The following method perfectly supports document writing.

The code is as follows Copy Code

if (!document.getelementsbyclassname) {
Document.getelementsbyclassname = function (ClassName, Element) {
var children = (Element | | document). getElementsByTagName (' * ');
var elements = new Array ();
for (var i=0; i<children.length; i++) {
var child = Children[i];
var classnames = Child.className.split (');
for (Var j=0 j<classnames.length; J + +) {
if (classnames[j] = = ClassName) {
Elements.push (child);
Break
}
}
}
return elements;
};
}

The ultimate solution is:

The code is as follows Copy Code

var getelementsbyclassname = function (Searchclass, Node,tag) {
if (document.getelementsbyclassname) {
var nodes = (Node | | document). GETELEMENTSBYCLASSNAME (searchclass), result = [];
for (var i=0 node = nodes[i++];) {
if (tag!== "*" && node.tagname = = Tag.touppercase ()) {
Result.push (node)
}else{
Result.push (node)
}
}
return result
}else{
node = node | | Document
Tag = Tag | | "*";
var classes = Searchclass.split (""),
elements = (Tag = = "*" && node.all)? Node.all:node.getElementsByTagName (TAG),
Patterns = [],
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) Result.push (current);
}
return result;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.