表格中鼠标移入变色

表格中鼠标移入变色

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <body>
        <table id="table" border="1" width="100%" align="center" style="text-align: center;">
            <tr>
                <td colspan="5" align="left">
                    <input id="uncheckBtn" type="button" value="全不选" />
                    <input id="reverseBtn" type="button" value="反选" />
                </td>
     
            </tr>
            <tr>
                <th>全选<input id="all" type="checkbox" /></th>
                <th>分类ID</th>
                <th>分类名称</th>
                <th>分类描述</th>
                <th>操作</th>
            </tr>
            <tr>
                <td><input type="checkbox" class="itemSelect" /></td>
                <td>1</td>
                <td>手机数码</td>
                <td>手机数码类商品</td>
                <td><a href="javascript:;">修改</a>|<a href="javascript:;">删除</a></td>
            </tr>
            <tr>
                <td><input type="checkbox" class="itemSelect" /></td>
                <td>2</td>
                <td>电脑办公</td>
                <td>电脑办公类商品</td>
                <td><a href="javascript:;">修改</a>|<a href="javascript:;">删除</a></td>
            </tr>
            <tr>
                <td><input type="checkbox" class="itemSelect" /></td>
                <td>3</td>
                <td>鞋靴箱包</td>
                <td>鞋靴箱包类商品</td>
                <td><a href="javascript:;">修改</a>|<a href="javascript:;">删除</a></td>
            </tr>
            <tr>
                <td><input type="checkbox" class="itemSelect" /></td>
                <td>4</td>
                <td>家居饰品</td>
                <td>家居饰品类商品</td>
                <td><a href="javascript:;">修改</a>|<a href="javascript:;">删除</a></td>
            </tr>
        </table>
    </body>
    
    <script>
        window.onload = function () {
     
            // getElementsByTagName(tr标签);
            // 1. 获取所有的 tr 标签
            var trs = document.getElementsByTagName("tr");
            // alert("trs.length = " + trs.length);
     
            // 定义一个 color 属性
            var color;
     
            // 2. 遍历 trs 数组
            for (var i = 2; i < trs.length; i++) {
     
                // 3. 判断
                if (i % 2 == 0) {
                    trs[i].style.backgroundColor = "yellow";
                } else {
                    trs[i].style.backgroundColor = "skyblue";
                }
     
                // 4. 为每一行绑定一个 `鼠标移入 onmouseover` 事件
                trs[i].onmouseover = function () {
                    // 记录当前行颜色
                    color = this.style.backgroundColor;
                    // trs[i].style.backgroundColor = "#ccc";  // 行不通. (在函数内部不能使用循环变量 i)
                    // 当前行实现变色
                    this.style.backgroundColor = "#ccc";
                }
     
                // 5. 为每一行绑定一个 `鼠标移出 onmouseout` 事件
                trs[i].onmouseout = function () {
                    this.style.backgroundColor = color;
                }
            }
        }
    </script>
    
    
    
    
    
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值