Skip to content

Instantly share code, notes, and snippets.

@xubo-bj
Last active March 22, 2016 03:04
Show Gist options
  • Select an option

  • Save xubo-bj/422d3b9fdd0248df01ed to your computer and use it in GitHub Desktop.

Select an option

Save xubo-bj/422d3b9fdd0248df01ed to your computer and use it in GitHub Desktop.
class
function hasClass(ele, cls) {
return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}
function addClass(ele, cls) {
if (!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