×

jQuery实现table隔行换色和鼠标经过变色的两种方法

作者:Terry2017.02.07来源:Web前端之家浏览:9848评论:0
关键词:JQueryJS
一、隔行换色
复制代码 代码如下:

$("tr:odd").css("background-color","#eeeeee");
$("tr:even").css("background-color","#ffffff");

或者一行搞定:
复制代码 代码如下:

$("table tr:nth-child(odd)").css("background-color","#eeeeee");

:nth-child 匹配其父元素下的第N个子或奇偶元素

二、鼠标经过变色
复制代码 代码如下:

$("tr").live({
mouseover:function(){
$(this).css("background-color","#eeeeee");
},
mouseout:function(){
$(this).css("background-color","#ffffff");
}
})

或者
复制代码 代码如下:

$("tr").bind("mouseover",function(){
$(this).css("background-color","#eeeeee");
})
$("tr").bind("mouseout",function(){
$(this).css("background-color","#ffffff");
})

当然live()和bind()都可以同时绑定多个事件或分开。

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/svg1486396800899.html

网友评论文明上网理性发言已有0人参与

发表评论: