<styletype= "Text/css">Div.testclass{Background-color:Gray; }</style><Scripttype= "Text/javascript">functionhasclass (obj, cls) {returnObj.className.match (NewRegExp ('(\\s|^)' +CLS+ '(\\s|$)'));}functionaddclass (obj, cls) {if (! This. Hasclass (obj, CLS)) Obj.classname+= " " +CLS;}functionremoveclass (obj, cls) {if(Hasclass (obj, cls)) {varReg= NewRegExp ('(\\s|^)' +CLS+ '(\\s|$)'); Obj.classname=Obj.className.replace (Reg,' '); }}functionToggleclass (obj,cls) {if(Hasclass (obj,cls)) {removeclass (obj, CLS); }Else{addclass (obj, CLS); }}functiontoggleclasstest () {varobj=document. getElementById ('Test'); Toggleclass (obj,"TestClass");}</Script><Body> <DivID= "Test"style= "width:250px;height:100px;">sssssssssssss</Div> <inputtype= "button"value= "Toggleclasstest"onclick= "toggleclasstest ();" /></Body>
1.addClass: Add a style to the specified DOM element
2.removeClass: Delete The style of the specified DOM element
3.toggleClass: Delete (add) a style if it exists (does not exist)
4.hasClass: Determine if the style exists
Native JS Implementation Addclass,removeclass,toggleclass