The simple implementation _javascript technique of acquiring class in JavaScript

Source: Internet
Author: User
Tags tag name tagname

JS did not get the class method, found some packaging good methods, here to tidy up

(1) Encapsulation first

Encapsulates 
      the GetClass function getclass (tagname,classname)//obtain a label named TagName, the element 
      { 
        if ( Document.getelementsbyclassname)//support this function 
        {return    document.getelementsbyclassname (className); 
        } 
        else 
        {    var tags=document.getelementsbytagname (tagName);//Get tag 
          var tagarr=[];//to return the element with a class name of classname For 
          (Var i=0;i < tags.length; i++) 
          { 
            if (Tags[i].class = = ClassName) 
            { 
              Tagarr[tagarr.length] = tags[i];//Save the element that satisfies the condition 
            } 
          } 
          return tagarr; 
        } 
      

(2) Main program

<ul>
  <li class= "Dictap" >1</li>
  <li class= "Dictap" >2</li>
  <li class= " Dictap ">3</li>
  <li class=" Dictap ">4</li>
</ul>     

(3) Get all Li content program with class as Dictap

Window.onload = function ()
{  var topmenus = getclass (' li ', ' Dictap ');
  for (Var i=0;i < topmenus.length i++)
  {
    alert (topmenus[i].innerhtml);    
  }

}

(4) Practical application in Project

Click on the custom attribute PersonID, open the Details page and carry the value across pages var Dictap = getclass (' li ', ' Dictap '); for (var i=0;i <dicTap.length; i++) {Dictap[i].addeventlistener (' tap ', function () {var 
            Personid=this.getattribute ("PersonId"); 
            Localstorage.a=personid; 
          Mui.openwindow ({url: ' disciplinedetail.html ', id: ' Disciplinedetail '}); 
        }//Package GetClass function GetClass (tagname,classname)//Get tag name TagName, class name ClassName element { 
        if (document.getelementsbyclassname)//support this function {return document.getelementsbyclassname (className); else {var tags=document.getelementsbytagname (tagName);//Get tag var tagarr=[];//used to return 
            Returns the element for the class name ClassName for (Var i=0;i < tags.length; i++) {if (Tags[i].class = = className) 
            {Tagarr[tagarr.length] = tags[i];//Save the element that satisfies the condition} return Tagarr; } 
      }

Above this JavaScript to get the class of simple implementation is a small series to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

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.