JavaScript in Document.getelementsbyclassname compatibility encapsulation method one

Source: Internet
Author: User
Tags tag name

varGetelmsbyclsname =function(ClassName, results) {results= Results | | []; //determine if the browser supports Getelementsbyclassname            if(document.getelementsbyclassname) {//browsers support this methodresults.push.apply (Results, Document.getelementsbyclassname (className)); } Else {                //Browser does not support                //implementation: Gets the elements of the page through the class name                //Ideas:                //1 Get all the elements through the tag name                //2 loops through all the acquired elements, judging whether the current element has no specified class                //1 Getting all the elements in the page                varnodes = document.getElementsByTagName ("*"); //2 times Calendar                 for(vari = 0; i < nodes.length; i++) {                    varCnodes =Nodes[i]; ///2.1 Determines whether the current element contains the specified class ClassName//The first way://1 Gets the class name to the current element classname/getattribut                    E ("class")///2 will get the class name separated by a space to produce an array//["C1", "C2", "C3"] 3 use each element in the array to compare with classname//4 if it is in accordance with the requirements, put it in results var cnodeclsname = Cnodes.class                    Name;                    var clsnames = Cnodeclsname.split ("");                            for (var j = 0; J < Clsnames.length; J + +) {if (clsnames[j] = = = ClassName) {                        Results.push (Cnodes); }                    }                }            }                        returnresults; };

JavaScript in Document.getelementsbyclassname compatibility encapsulation method one

Related Article

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.