Skip to content

Instantly share code, notes, and snippets.

@LESTADru
Created February 4, 2014 17:39
Show Gist options
  • Save LESTADru/8808543 to your computer and use it in GitHub Desktop.
Save LESTADru/8808543 to your computer and use it in GitHub Desktop.
функция removeClass(obj, cls), которая удаляет класс cls, если он есть
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>функция removeClass(obj, cls), которая удаляет класс cls, если он есть</title>
</head>
<body>
<script>
'use strict'
function removeClass(obj, cls){
var arr = obj.className ? obj.className.split(" "):[];
//Ищем в массиве нужный нам класс. Если находим его, то удаляем.
for(var i=0;i<arr.length;i++){
if(arr[i] == cls){
arr.splice(i,1);
i --;
}
}
obj.className = arr.join(' ');
}
var obj = {
className: 'my menu menu'
}
removeClass(obj, 'menu');
alert(obj.className);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment