【jQuery小白】 问关于鼠标悬停选择标签的问题

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="http://how2j.cn/study/js/jquery/2.0.0/jquery.min.js"></script>
    <link href="http://how2j.cn/study/css/bootstrap/3.3.6/bootstrap.min.css" rel="stylesheet">
    <script src="http://how2j.cn/study/js/bootstrap/3.3.6/bootstrap.min.js"></script>
</head>
<body>

<table class="table">
    <thead>
    <th>头像</th>
    <th>名字</th>
    <th>HP</th>
    </thead>
    <tbody>
    <tr>
        <td><img width="20px" src="http://how2j.cn/study/bootstrap/lol/gareen.png"/></td>
        <td>盖伦</td>
        <td >616</td>
    </tr>
    <tr>
        <td ><img width="20px" src="http://how2j.cn/study/bootstrap/lol/teemo.png"/></td>
        <td >提莫</td>
        <td >383</td>
    </tr>
    <tr>
        <td><img width="20px" src="http://how2j.cn/study/bootstrap/lol/akali.png"/></td>
        <td>阿卡丽</td>
        <td>448</td>
    </tr>
    </tbody>
</table>


<script>
    $("tr").mouseover(function(){
        $("tr>td").addClass("success")
    });

    $("tr").mouseout(function(){
        $("tr>td").removeClass("success")
    });
</script>
</body>
</html>

运行如下:

clipboard.png

鼠标移动到表格上面:

clipboard.png

所有的都变色
我想做到的是,移动到某一行,变某一行的颜色,但是不知道如何选择单个的tr,而不是所有的tr
不知道这个应该怎么弄?

阅读 2.5k
4 个回答

$('tr>td')选择的是全部的

    <script>
        $("tr").mouseover(function () {
            $(this).addClass("success")
        });

        $("tr").mouseout(function () {
            $(this).removeClass("success")
        });
    </script>

clipboard.png

clipboard.png

用 css 就可以实现

tr:hover {
  background-color: green;
}
$("tr").mouseover(function(){
        $("tr->td").removeClass("success");
         $(this).find('td').add("success")
    });

    $("tr").mouseout(function(){
        $("tr->td").removeClass("success")
    });

你其实可以用hover方法 或者css的hover伪类

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题