Created
April 22, 2015 22:11
-
-
Save anonymous/3ed3e71bafb6af4f3cab to your computer and use it in GitHub Desktop.
This file contains 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(node, className){ | |
var classes = node.getAttribute('class'); | |
if( classes === '' || undefined ){ | |
return false; | |
}else{ | |
classes = classes.split(/\s/); | |
for(var i=0; i < classes.length; i++){ | |
if(classes[i] === className){ | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
function addClass(node, className){ | |
var oldClass = node.getAttribute('class'); | |
if(oldClass === '' || undefined){ | |
node.setAttribute('class', className); | |
}else{ | |
node.setAttribute('class', oldClass + ' ' + className); | |
} | |
return true; | |
} | |
function removeClass(node, className){ | |
var classes = node.getAttribute('class'); | |
var noClass = function(){ | |
console.log('Такой класс отсутствует, нечего удалять.'); | |
return false; | |
} | |
if( classes === '' || undefined ){ | |
noClass(); | |
}else{ | |
classes = classes.split(/\s/); | |
var index = classes.indexOf(className); | |
if(index === -1){ | |
noClass(); | |
}else{ | |
classes.splice(index, 1); | |
classes = classes.join(' '); | |
node.setAttribute('class', classes); | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment