Native javascript implements addClass, removeClass, hasClass function, addclasshasclass
Native addClass, removeClass, hasClass function, no jquery
Class. js
Function hasClass (ele, cls) {return ele. className. match (new RegExp ('(\ s | ^)' + cls + '(\ s | $)');} function addClass (ele, cls) {if (! This. hasClass (ele, cls) ele. className + = "" + cls;} function removeClass (ele, cls) {if (hasClass (ele, cls )) {var reg = new RegExp ('(\ s | ^)' + cls + '(\ s | $)'); ele. className = ele. className. replace (reg, '') ;}// call the functionsaddClass (document. getElementById ("test"), "test"); removeClass (document. getElementById ("test"), "test") if (hasClass (document. getElementById ("test"), "test") {// do something };
Articles you may be interested in:
- Function Code of addClass, removeClass, and hasClass implemented by js
- Use JS to implement jQuery's addClass, removeClass, and hasClass Functions