1、鼠标事件经过onmouseover,鼠标离开onmouseout
2、鼠标经过,当前行高亮,鼠标离开,当前行背景变灰
3、第一行不变。thead。tbody
注意必须使用this来指代才有效果
<!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 {
width: 405px;
margin: 0 auto;
margin-top: 20px;
/* border-spacing: 0px; */
border-collapse: collapse;
text-align: center;
}
table thead {
color: blue;
background-color: skyblue;
padding-right: 10px
}
.tu a {
width: 13px;
height: 16px
}
.tu img {
width: 13px;
height: 13px
}
tr {
border: 1px solid grey;
border-left: 0px;
border-right: 0px;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline
}
.bg {
background-color: blue;
}
</style>
</head>
<body>
<table>
<thead>
<td>代码</td>
<td>名称</td>
<td>最新价</td>
<td>股吧</td>
</thead>
<tbody>
<tr>
<td><a href="">04338</a></td>
<td><a href="">微软-T</a></td>
<td>1600.00</td>
<td>
<a href=""><img src="linkto_ba.gif" alt=""></a>
</td>
</tr>
<tr>
<td><a href="">04338</a></td>
<td><a href="">微软-T</a></td>
<td>1600.00</td>
<td class="tu">
<a href=""><img src="linkto_ba.gif" alt=""></a>
</td>
</tr>
<tr>
<td><a href="">04338</a></td>
<td><a href="">微软-T</a></td>
<td>1600.00</td>
<td>
<a href=""><img src="linkto_ba.gif" alt=""></a>
</td>
</tr>
</tbody>
</table>
<script>
var tr = document.querySelector('tbody').querySelectorAll('tr')
console.log(tr)
//注册事件 利用循环
for (var i = 0; i < tr.length; i++) {
tr[i].onmouseover = function() {
console.log('11')
this.className = 'bg'
}
tr[i].onmouseout = function() {
this.className = ''
}
}
</script>
</body>
</html>
thead与tbody分离