Copy codeThe Code is as follows: window. onload = function (){
Var gaga = document. getElementById ("gaga ");
AddClass (gaga, "gaga1 ")
AddClass (gaga, "gaxx ");
RemoveClass (gaga, "gaga1 ")
RemoveClass (gaga, "gaga ")
Function hasClass (elements, cName ){
Return !! Elements. className. match (new RegExp ("(\ s | ^)" + cName + "(\ s | $)"); // (\ s | ^) determine whether there is a space (\ s | $) in the front to determine whether there are spaces in the back. Two exclamation points are converted to boolean values for easy judgment.
};
Function addClass (elements, cName ){
If (! HasClass (elements, cName )){
Elements. className + = "" + cName;
};
};
Function removeClass (elements, cName ){
If (hasClass (elements, cName )){
Elements. className = elements. className. replace (new RegExp ("(\ s | ^)" + cName + "(\ s | $)"), ""); // replace
};
};
};