<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk"/>
<script type="text/javascript">
window.onload = function(){
// 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。
var trs = document.getElementsByTagName("tr");
for(var tr of trs){
tr.onmouseover = function(){
this.style.backgroundColor = "#f2f2f2";
}
tr.onmouseout = function(){
this.style.backgroundColor = "#fff";
}
}
}
</script>
</head>
<body>
<table border="1" width="50%" id="table">
<tr>
<th>学号</th>
<th>姓名</th>
<th>操作</th>
</tr>
<tr>
<td>xh001</td>
<td>王小明</td>
</tr>
<tr>
<td>xh002</td>
<td>刘小芳</td>
</tr>
</table>
</body>
</html>
html table标签鼠标移进移出背景变色
最新推荐文章于 2024-02-28 10:42:02 发布
本文介绍了一种使用JavaScript实现的鼠标悬停时表格行背景颜色切换的效果,通过为表格的每一行绑定鼠标移上和移除事件,可以轻松地改变行的背景色,提升用户体验。
386

被折叠的 条评论
为什么被折叠?



