Getelementsbyclassname compatible with IE 8

Source: Internet
Author: User

1 functiongetelements (node, className, item) {2         if(node.getelementsbyclassname) {3             returnNode.getelementsbyclassname (ClassName) [item];4}Else {5             varResults =NewArray ();6             varallele = node.getelementsbytagname (' * '));7              for(vari = 0, L = allele.length; I < L; i++) {8                 if(Allele[i].classname.indexof (className)! =-1) {9 Results.push (Allele[i]);Ten                 } One             } A             returnResults[item]; -         } -}

ClassName represents the class name of the element node to get, item represents the ordinal of the element node to get (default starting from 0)

Instance:

1 <ul>2         <li class= "Li-element" >111</li>3         <li class= " Li-element ">222</li>4         <li class=" Li-element ">333</li>5         <li class= "Li-element" >444</li>6     </ul>

To get the third Li element in UL node, first get the UL element node, var ulelement=document.getelementsbytagname (' ul ') [0];

Then get the third Li node, var lielement=getelementsbyclassname (ulelement, ' li-element ', 2);

Bo Master is learning JavaScript, if there are shortcomings, hope to point out.

Getelementsbyclassname compatible with IE 8

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.