Created
July 27, 2012 09:33
-
-
Save simplelife7/3187105 to your computer and use it in GitHub Desktop.
【JS】操作class
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
function hasClass(el, name) { | |
return new RegExp('(\\s|^)'+name+'(\\s|$)').test(el.className); | |
} | |
function addClass(el, name) | |
{ | |
if (!hasClass(el, name)) { el.className += (el.className ? ' ' : '') +name; } | |
} | |
function removeClass(el, name) | |
{ | |
if (hasClass(el, name)) { | |
el.className=el.className.replace(new RegExp('(\\s|^)'+name+'(\\s|$)'),' ').replace(/^\s+|\s+$/g, ''); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment