Created
February 4, 2014 17:39
-
-
Save LESTADru/8808543 to your computer and use it in GitHub Desktop.
функция removeClass(obj, cls), которая удаляет класс cls, если он есть
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
<!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