On the problem-solving method of getelementsbyclassname in IE _javascript skills

Source: Internet
Author: User
Today, the use of Getelementsbyclassname wrote a small program, full of joy ready to test, in FF, Google and other mainstream browsers on the test is no problem, in the IE9 also no problem, in the IE6, 8 test when there is a problem, the browser error. Tangled up the code, seemingly no problem, determined to find the Niang ... Finally found the problem, it is IE6, 8 does not support getelementsbyclassname this method. Then on the Internet to search for solutions, found a better way, is a foreigner to write a method, looking at incredibly is the 05 problem ... First code:
Copy Code code as follows:

*
Developed by Robert Nyman, http://www.robertnyman.com
code/licensing:http://code.google.com/p/getelementsbyclassname/
*/
var getelementsbyclassname = function (className, tag, elm) {
if (document.getelementsbyclassname) {
Getelementsbyclassname = function (className, tag, elm) {
Elm = Elm | | Document
var elements = Elm.getelementsbyclassname (ClassName),
NodeName = (tag)? New RegExp ("\\b" + tag + "\\b", "I"): null,
Returnelements = [],
Current
For (Var i=0, il=elements.length i<il; i+=1) {
current = Elements[i];
if (!nodename | | nodename.test (current.nodename)) {
Returnelements.push (current);
}
}
return returnelements;
};
}
else if (document.evaluate) {
Getelementsbyclassname = function (className, tag, elm) {
Tag = Tag | | "*";
Elm = Elm | | Document
var classes = Classname.split (""),
Classestocheck = "",
Xhtmlnamespace = "http://www.w3.org/1999/xhtml",
Namespaceresolver = (Document.documentElement.namespaceURI = = = Xhtmlnamespace)? Xhtmlnamespace:null,
Returnelements = [],
Elements
Node
For (Var j=0, jl=classes.length j<jl; j+=1) {
Classestocheck + = "[Contains (Concat (', @class, '), '" + classes[j] + ")]";
}
try {
elements = Document.evaluate (".//" + tag + Classestocheck, Elm, namespaceresolver, 0, NULL);
}
catch (e) {
elements = Document.evaluate (".//" + tag + classestocheck, elm, NULL, 0, NULL);
}
while (node = Elements.iteratenext ())) {
Returnelements.push (node);
}
return returnelements;
};
}
else {
Getelementsbyclassname = function (className, tag, elm) {
Tag = Tag | | "*";
Elm = Elm | | Document
var classes = Classname.split (""),
Classestocheck = [],
elements = (Tag = = "*" && elm.all)? Elm.all:elm.getElementsByTagName (TAG),
Current
Returnelements = [],
Match
For (Var k=0, kl=classes.length k<kl; k+=1) {
Classestocheck.push (New RegExp ("(^|\\s)" + Classes[k] + "(\\s|$)"));
}
For (Var l=0, ll=elements.length l<ll; l+=1) {
current = Elements[l];
Match = false;
For (Var m=0, ml=classestocheck.length m<ml; m+=1) {
Match = Classestocheck[m].test (current.classname);
if (!match) {
Break
}
}
if (match) {
Returnelements.push (current);
}
}
return returnelements;
};
}
Return Getelementsbyclassname (className, tag, elm);
};

How to use it can go to see how it is used inside.

Use this method, IE6, 8 can show, but still have a random problem, sometimes refresh several times will be an error, temporarily no solution. Then I want to use jquery can solve the problem of the refresh error, of course, jquery can also replace getelementsbyclassname this method, concrete how to use here is not introduced. However, the fact can not be as people would like to refresh or there will be problems, and the error is random, sometimes the first load process on the error, sometimes a few times to refresh the error. Heroes. Solve.

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.