Created
May 15, 2017 07:16
-
-
Save dmjcomdem/7fb2a75554568fa256f262c48302d4a3 to your computer and use it in GitHub Desktop.
Функция для задания/ удаления класса addClass(elem, class) removeClass(elem, 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
/* | |
* @param {DOM Element Object} o | |
* @param {String} c name class | |
*/ | |
function addClass(o, c) { | |
var re = new RegExp("(^|\\s)" + c + "(\\s|$)", "g"); | |
if (re.test(o.className)) return | |
o.className = (o.className + " " + c).replace(/\s+/g, " ").replace(/(^ | $)/g, ""); | |
} | |
function removeClass(o, c){ | |
var re = new RegExp("(^|\\s)" + c + "(\\s|$)", "g"); | |
o.className = o.className.replace(re, "$1").replace(/\s+/g, " ").replace(/(^ | $)/g, ""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment