表格中鼠标移入变色
<!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>