Created
April 5, 2012 12:44
-
-
Save h4/2310793 to your computer and use it in GitHub Desktop.
Функции для работы с именами классов
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
/* | |
* Служебные функции для работы с css-классами | |
* принимают ссылку на элемент странцы и строковое имя класса | |
*/ | |
// Добавление класса к объекту | |
function addClass(el, cls) { | |
var c = el.className.split(' '); | |
for(var i=0; i < c.length; i++) { | |
if (c[i] == cls) return; | |
} | |
c.push(cls); | |
el.className = c.join(' '); | |
} | |
// Удаление класса у объекта | |
function removeClass(el, cls) { | |
var c = el.className.split(' '); | |
for(var i=0; i < c.length; i++) { | |
if (c[i] == cls) c.splice(i--, 1); | |
} | |
el.className = c.join(' '); | |
} | |
// Проверка наличия класса у объекта | |
function hasClass(el, cls) { | |
for(var c = el.className.split(' '),i=c.length-1; i>=0; i--) { | |
if (c[i] == cls) return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment