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