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

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

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

下面放代码

<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实现鼠标移动到某一行上面的时候那一行变色的这个效果

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过设置 `rowClassName` 属性来实现选中一行变色的效果。具体实现方式如下: 1. 首先,在表格组件中添加 `rowClassName` 属性,该属性接受一个函数作为参数,用于判断行是否被选中。示例如下: ```html <el-table :data="tableData" :row-class-name="tableRowClassName"> ``` 2. 在 Vue 实例中定义 `tableRowClassName` 方法,该方法接收一个参数 `row`,表示当前行的数据。在该方法中,可以判断该行是否被选中,并返回对应的类名。示例如下: ```javascript methods: { tableRowClassName({row, rowIndex}) { if (rowIndex === this.selectedIndex) { return 'selected-row'; // 选中行的类名 } return ''; } } ``` 其中,`this.selectedIndex` 表示当前选中的行的索引。如果当前行的索引与选中行的索引相等,则返回 `'selected-row'` 类名,否则返回空字符串。 3. 在选中行时,设置 `selectedIndex` 的值,触发表格组件重新渲染,实现选中一行变色的效果。示例如下: ```javascript methods: { handleRowClick(row, column, event) { this.selectedIndex = row.index; } } ``` 在上述代码中,`handleRowClick` 方法用于处理行点击事件,当用户点击某一行时,设置 `selectedIndex` 的值为当前行的索引,从而触发表格组件重新渲染。 最后,通过 CSS 样式设置选中行的背景色即可。例如,可以在样式文件中添加如下代码: ```css .selected-row { background-color: #f5f7fa; } ``` 这样,当用户选中一行时,该行的背景颜色成灰色,从而实现选中一行变色的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值