使用javascript实现表格高亮(onmouseover()鼠标被移动元素上事件onmouseout()鼠标从某元素移开事件)
技术分析
- 确定事件(onmouseover()鼠标被移动元素上事件onmouseout()鼠标从某元素移开事件)
代码步骤
- 首先确定事件onmouseover()鼠标移动到元素上触发事件,onmouseout()鼠标从某元素移开触发事件
- 书写这两个函数,设置元素属性即可。
代码事件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>使用javascript实现高亮</title>
<script type="text/javascript">
function movemousever(val){
document.getElementById("tr"+val).style.backgroundColor="red";
}
function movemouseout(val){
document.getElementById("tr"+val).style.backgroundColor="white";
}
</script>
</head>
<body>
<table border="1px" width="500px" height="300px" align="center">
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr id="tr1" onmouseover="movemousever(1)" onmouseout="movemouseout(1)">
<td>1</td>
<td>小明</td>
<td>20</td>
</tr>
<tr id="tr2" onmouseover="movemousever(2)" onmouseout="movemouseout(2)">
<td>2</td>
<td>小李</td>
<td>21</td>
</tr>
<tr id="tr3" onmouseover="movemousever(3)" onmouseout="movemouseout(3)">
<td>3</td>
<td>小孙</td>
<td>33</td>
</tr>
<tr id="tr4" onmouseover="movemousever(4)" onmouseout="movemouseout(4)">
<td>4</td>
<td>小凤</td>
<td>23</td>
</tr>
<tr id="tr5" onmouseover="movemousever(5)" onmouseout="movemouseout(5)">
<td>5</td>
<td>小肖</td>
<td>26</td>
</tr>
<tr id="tr6" onmouseover="movemousever(6)" onmouseout="movemouseout(6)">
<td>6</td>
<td>小绿</td>
<td>27</td>
</tr>
</tbody>
</table>
</body>
</html>
页面效果展示