Skip to content

Instantly share code, notes, and snippets.

Created April 22, 2015 22:11
Show Gist options
  • Save anonymous/3ed3e71bafb6af4f3cab to your computer and use it in GitHub Desktop.
Save anonymous/3ed3e71bafb6af4f3cab to your computer and use it in GitHub Desktop.
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