Add, search, and delete elements using JavaScript.
The code is very simple, so there is not much nonsense here.
<! DOCTYPE html>
Example 2:
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 to convert the Two exclamation points into 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 method: replace };};};
The above is all the content of this article. I hope you will like it.