參考頁面:
http://www.w3school.com.cn/jquery/jquery_ref_attributes.asp
1:addClass() romoveclass():向匹配的元素添加指定的類名 一個或多個,致死天際一個或多個class屬性:(如果添加多個類,可以使用空格分隔類名)
A:想第一個p元素添加一個類:
$("p:first").addClass("intro");
B: 使用函數添加一個類
<script type=
"
text/javascript
">
$(document).ready(function(){
$( " button ").click(function(){
$( ' p ').addClass(function(n){
return ' par_ ' + n;
});
});
});
</script>
<style type= " text/css ">
.par_0
{
color:blue;
}
.par_1
{
color:red;
}
</style>
$(document).ready(function(){
$( " button ").click(function(){
$( ' p ').addClass(function(n){
return ' par_ ' + n;
});
});
});
</script>
<style type= " text/css ">
.par_0
{
color:blue;
}
.par_1
{
color:red;
}
</style>
C :向被選元素中添加兩個類: 多個類用空格隔開就可以:
$("p:first").addClass("intro note");
D:改變元素的類:使用 addClass() 和 removeClass() 來移除 class,並添加新的 class。
$("p:first").removeClass("intro").addClass('main');
2:
hasclass():
檢查被選元素是否包含指定的 class 包含返回true:不包含返回false
alert($("p:first").hasClass("intro")
);
3:toggleclass() : 對設置或移除被選元素的一個或多個類進行切換:可進行多次切換
$("p").toggleClass("main")
;