<!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>
<style>
table {
border-collapse: collapse;
width: 400px;
text-align: center;
}
thead {
background: palegoldenrod;
}
table,
tr,
td,
th {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>姓名</th>
<th>学号</th>
<th>班级</th>
</tr>
</thead>
<tbody>
<tr>
<td>张三</td>
<td>李四</td>
<td>王五</td>
</tr>
<tr>
<td>小明</td>
<td>小红</td>
<td>小丑</td>
</tr>
<tr>
<td>小明</td>
<td>小红</td>
<td>小丑</td>
</tr>
</tbody>
</table>
</body>
<script>
var tbody = document.querySelector("tbody")
for (let i = 0; i < tbody.children.length; i++) {
tbody.children[i].addEventListener("mouseenter", function () {
for (let i = 0; i < tbody.children.length; i++) {
tbody.children[i].style.backgroundColor=''
}
this.style.backgroundColor = "pink"
})
tbody.children[i].addEventListener("mouseleave", function () {
this.style.backgroundColor = ""
})
}
</script>
</html>
html表格隔行变色
最新推荐文章于 2022-10-03 19:46:52 发布