- addClass(): adds the class passed as an argument
- removeClass(): removes the class or list of classes passed in. If it is called without an argument, all classes are removed;
- hasClass(): check if an element has a class (returns true or false)
- toggleClass(): Adds a class if it is not there, remove it if it is.
Ex.:
Write two functions:
- toggleClass which takes a jQuery object and the name of the class and calls toggleClass on the object
- setExclusiveClass which also takes a jQuery object and a class, and makes it such that the elements in the object passed have only the class given.
1.
function toggleClass($that,className) {
$that.toggleClass(className);
}
2.
function setExclusiveClass($that,className) {
$that.removeClass();
return $that.addClass(className);
}
_____________________________________________________________________________________
Each()
Метод .each() производит обход всех элементов, содержащихся в наборе jQuery и вызывает функцию обратного вызова callback для каждого из них. Не путать с функцией jQuery.each().
$collection.each(f);. $collection is an array, and f is a function that does something on each element in the array.
1. Примечание:
Цикл можно остановить в любой момент, вернув из функции обратного вызова false.
$('li').each(function(i,elem) {
if ($(this).is(".stop")) {
alert("Остановлено на " + i + "-м пункте списка.");
return false;
} else {
alert(i + ': ' + $(elem).text());
});
_____________________________________________________________________________________