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