首页 > 语言 > JavaScript > 正文

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

2024-05-06 16:07:04
字体:
来源:转载
供稿:网友
这篇文章主要介绍jQuery实现table隔行换色和鼠标经过变色的两种方法,需要的朋友可以参考下

一、隔行换色

复制代码 代码如下:


$("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()都可以同时绑定多个事件或分开。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选