.removeClass()
.removeClass( [ className ] ) 返回: jQuery
描述: 移除每個(gè)匹配元素的一個(gè),多個(gè)或全部樣式。
-
version added: 1.0.removeClass( [ className ] )
className為每個(gè)匹配元素移除的樣式屬性名。
-
version added: 1.4.removeClass( function(index, class) )
function(index, class)這個(gè)函數(shù)返回一個(gè)或更多用空格隔開的被移除樣式名。接收元素的索引位置和元素舊的樣式名作為參數(shù)。
如果一個(gè)樣式類名作為一個(gè)參數(shù),只有這樣式類為匹配的元素集合中被刪除 。 如果沒(méi)有樣式名作為參數(shù),那么所有的樣式類將被移除。
從所有匹配的每個(gè)元素中同時(shí)移除多個(gè)用空格隔開的樣式類 ,像這樣:
$('p').removeClass('myClass yourClass')
這個(gè)方法通常和 .addClass() 一起使用用來(lái)切換元素的樣式, 像這樣:
$('p').removeClass('myClass noClass').addClass('yourClass');
這里從所有段落刪除 myClass 和 noClass 樣式類, 然后 yourClass 樣式被添加。
用其他樣式類替換現(xiàn)有的樣式類,我們可以使是有 .attr('class', 'newClass') 替換.
在 jQuery 1.4中, .removeClass() 方法允許我們通過(guò)函數(shù)來(lái)傳遞刪除樣式名。
$('li:last').removeClass(function() {
return $(this).prev().attr('class');
});
這個(gè)例子從最后一個(gè) <li> 中移除來(lái)自倒數(shù)第二的樣式名。
舉例:
例子:從匹配的元素中移除“blue”樣式類。
<!DOCTYPE html>
<html>
<head>
<style>
p { margin: 4px; font-size:16px; font-weight:bolder; }
.blue { color:blue; }
.under { text-decoration:underline; }
.highlight { background:yellow; }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<p class="blue under">Hello</p>
<p class="blue under highlight">and</p>
<p class="blue under">then</p>
<p class="blue under">Goodbye</p>
<script>$("p:even").removeClass("blue");</script>
</body>
</html>
Demo:
例子:從匹配的元素中移除“blue”和“under”樣式類。
<!DOCTYPE html>
<html>
<head>
<style>
p { margin: 4px; font-size:16px; font-weight:bolder; }
.blue { color:blue; }
.under { text-decoration:underline; }
.highlight { background:yellow; }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<p class="blue under">Hello</p>
<p class="blue under highlight">and</p>
<p class="blue under">then</p>
<p class="blue under">Goodbye</p>
<script>$("p:odd").removeClass("blue under");</script>
</body>
</html>
Demo:
例子:從匹配的元素中移除所有樣式類。
<!DOCTYPE html>
<html>
<head>
<style>
p { margin: 4px; font-size:16px; font-weight:bolder; }
.blue { color:blue; }
.under { text-decoration:underline; }
.highlight { background:yellow; }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<p class="blue under">Hello</p>
<p class="blue under highlight">and</p>
<p class="blue under">then</p>
<p class="blue under">Goodbye</p>
<script>$("p:eq(1)").removeClass();</script>
</body>
</html>