鼠标移到表格的某一行,那一行变色的问题

本文介绍了一种使用JavaScript和CSS实现表格行鼠标悬停时背景颜色变化的方法。通过为表格行添加事件监听器并在鼠标移入移出时更改其样式类来实现这一效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个实现的过程很简单,网上相关实现的例子很多,这里记录下自己之前实现的方法。参考的网上的例子,网址不记得了。

 这个问题简单点就是鼠标响应事件。

下面放代码

<html>

<title></title>

<head>

<script type="text/javascript">
    var rows = document.getElementsByTagName('tr');//取得行
        for(var i=0 ;i<rows.length; i++)
        {
            rows[i].onmouseover = function(){//鼠标移上去,添加一个类'hilite'
                this.className += 'hilite';
            }
            rows[i].onmouseout = function(){//鼠标移开,改变该类的名称
                this.className = this.className.replace('hilite','');
            }
        }
    </script>
        
        <style type="text/css">
            .table1 tr:hover,.table1 tr.hilite
            {
            background-color:#99FF66;
            color:#0000CC;
            }
        </style>
    </head>
  <body>
           <form action="">
               
               <table id="table1" class="table1">
                   <tr>
                       <th>姓名</th>
                       <th>年龄</th>
                       <th>性别</th>
                   </tr>
                   <tr>
                       <td>小红</td>
                       <td>12</td>
                       <td>女</td>
                   </tr>
                   <tr>
                       <td>小明</td>
                       <td>14</td>
                       <td>男</td>
                   </tr>
               </table>
               <table id="table2" class="table1">
                   <tr>
                       <th>国家</th>
                       <th>身份</th>
                       <th>性别</th>
                   </tr>
                   <tr>
                       <td>中国</td>
                       <td>将军</td>
                       <td>女</td>
                   </tr>
                   <tr>
                       <td>美国</td>
                       <td>部长</td>
                       <td>男</td>
                   </tr>
               </table>
           </form>
  </body>
</html>


上面就是针对指定的table实现鼠标移动到某一行上面的时候那一行变色的这个效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值