1 function Getbyclass (oparent,sclass) {2 if(oparent.getelementsbyclassname) {3 returnoparent.getelementsbyclassname (sclass);4}Else{5 varArr=[];6 varAele=oparent.getelementsbytagname ('*');7 varRe=NewREGEXP ('\\b'+sclass+'\\b');8 9 for(varI=0; i<aele.length;i++){Ten if(re.test (aele[i].classname)) { one Arr.push (aele[i]); a } - } - returnarr; the } - } - - + function Hasclass (obj,sclass) { - varRe=NewREGEXP ('\\b'+sclass+'\\b'); + returnre.test (obj.classname); a } at - - function AddClass (obj,sclass) { - //<div class= ' ac now active ' ></div> - //there is no class by itself - if(obj.classname) { in //judging the added class. - if(!Hasclass (OBJ,SCLASS)) { toobj.classname+=' '+sclass; + } -}Else{ theObj.classname=sclass; * } $ }Panax Notoginseng - the function Removeclass (obj,sclass) { + varRe=NewREGEXP ('\\b'+sclass+'\\b'); a if(hasclass (OBJ,SCLASS)) { theObj.classname=obj.classname.replace (re,"'). Replace (/^\s+|\s+$/g,"'). Replace (/\s+/g,' '); + } -}
The native JS uses the regular realization Removeclass hasclass Getsclass addclass.