JS in the method of getting a tag class to seal a library

Source: Internet
Author: User

In JS we will often use, get the ID of a tag

var aid=document.getelementbyid ("id")

Now, although there is a getelementsbyclassname this method, but this method again IE6 compatibility problems, so insurance or a library to get class

First look at the library

/** * Created by ASUS on 2016/12/4 by Dirk_jian.*/functionGetbyclass (oparent,sclass) {varAele=oparent.getelementsbytagname (' * ')); vararesult=[]; varRe=NewREGEXP (' \\b ' +sclass+ ' \\b ', ' I ');  for(vari=0;i<aele.length;i++){//if (aele[i].classname==sclass)//If you have more than one class then you can't detect it.//if (Aele[i].classname.search (sclass)!=-1)//!=-1 proves that it has SCALSS, but only a few letters of class can be done, not fully achieve the purpose        if(Re.test (Aele[i].classname))//This is the detection of unrestricted range {Aresult.push (aele[i]); }    }    returnAresult;}

Then see how to invoke and modify the CSS style of the obtained class tag.

<script src= "Getbyclass.js" ></script> <script>window.onload=function() {Oul=document.getelementbyid ("Div1"); varAdiv1=getbyclass (Oul, ' div2 ')); varAdiv2=getbyclass (Oul, ' Div3 '));  for(vari=0;i<adiv1.length;i++) {Adiv1[i].style.border= ' 1px solid black '; Adiv1[i].style.width= ' 100px '; Adiv1[i].style.height= ' 100px '; }             for(vari=0;i<adiv2.length;i++) {Adiv2[i].style.width= ' 100px '; Adiv2[i].style.height= ' 100px '; Adiv2[i].style.border= ' 1px solid pink '; }        }    </script>

The following are HTML

<Body>    <DivID= "Div1">        <Divclass= "Div2"></Div>        <Divclass= "Div3"></Div>    </Div></Body>

This makes it easy to get the class of a tag.

JS in the method of getting a tag class to seal a library

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.