明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

jQuery实现table隔行换色和鼠标经过变色

Posted on 2020-05-07 22:16  且行且思  阅读(371)  评论(0编辑  收藏  举报

一、隔行换色

    $("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()都可以同时绑定多个事件或分开。