class encapsulation problem of document.getElementByClassName


Encapsulate your own class class   so that the browser recognizes the method of obtaining the element by className

  Principle:  (Core)

   We need to take out all the boxes and  use the traversal method  to  judge by the className  of each box . Leave if equal.

< script > window . onload  function (){ // Encapsulate your own class name function  getClass ( classname ){ // If the browser supports it, return directly if ( document . getElementsByClassName )             { return  document . getElementsByClassName ( classname );             } //  Unsupported browsers var  arr  [];  //  Array used to store satisfied var  dom 
    
       
        
            
            

                

             
            
            document.getElementsByTagName("*");//alert(dom.length);for(var i=0;i<dom.length;i++)            {if(dom[i].className == classname)                {arr.push(dom[i]);                }            }return arr;        }
            
            

                

                    


            

        console.log(getClass("demo").length);    }</script>

 

  end, split version

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324584841&siteId=291194637