-
-
Save webkupas/adbe44a7b39895699fc1e7f837afac63 to your computer and use it in GitHub Desktop.
JavaScript hasClass(), addClass(), removeClass(). #javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // class manipulation from http://www.openjs.com/scripts/dom/class_manipulation.php | |
| 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,' '); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment