ClassName related to Js-dom-css

Source: Internet
Author: User
Tags tagname

1, in non-standard browser, IE8 and the following browser does not support classname operation, including Getelementbyclassname,addclassname,removeclassname;

2, Getelementbyclassname processing compatible method:

  

varAli=getelementbyclassname (document, ' box ');//if more than one limiting element is in the form of a range: Getelementbyclassname (document, ' box ', ' Li ');functionGetelementbyclassname (parent,classname) {//The parent refers to the selected node in the range, classname refers to the classname name to get      varAels=parent.getelementbytagname (' * ');//gets all the elements of the page, or you can replace the "*" with the desired element range by means of a parameter transfer. For example, if you want to get all the LI elements of ClassName box, you need to change "*" to Li, add a tagname to the parameter section, and pass the parameters of TagName to Li. //Getelementbyclassname (parent,classname,tagname);//var aels=parent.getelementbytagname (tagName);      varArr=[];  for(vari=0;i<aels.length;i++){         varAclassname=aels[i].classname.split (' ');  for(varj=0;j<aclassname.length;j++){               if(aclassname[j]==className)                   {Arr.push (aels[i]);  Break; }        }      }}  

3, add ClassName Method: AddClass ()

 functionaddclass (obj,classname) {if(obj.classname== "){      //If it turns out that there is no classnameObj.classname=ClassName; }Else{      //If there is classname     //if the classname to be added does not exist in the original classname           varArrclassname=obj.classname.split (' '); varindex=Arrindexof (arrclassname,classname); if(Index==-1) {Obj.classname+ = "+ClassName; }     }     //if the classname that you want to add exists in the original classname, you do not have to add the} functionArrindexof (Arrclassname, className) { for(vari=0;i<arrclassname.length;i++){            if(a[i]==className) {               returni; }        }       return-1;} 

4, delete the ClassName method: Removeclass

functionRemoveclass (obj,classname) {//if the original class      if(obj.classname!= "){        varArrclassname=obj.classname.split (' '); varindex=Arrindexof (arrclassname.classname); //If you have a class that you want to remove        if(Index!=-1) {arrclassname.splice (index,1); Obj.classname=arrclassname.join (' '); }        //If there is no class to remove, no action is needed      }}         functionArrindexof (Arrclassname, className) { for(vari=0;i<arrclassname.length;i++){            if(a[i]==className) {               returni; }        }       return-1;} 

ClassName related to Js-dom-css

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.