当鼠标移动到表格的一行时这一行的背景颜色发生改变
方法一:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>当鼠标经过表格的一行时这一行的背景颜色发生改变</title>
<style type="text/css">
table, th, tr, td{
width:500px;
border:1px solid red;
border-collapse:collapse;
}
tr, td{
text-align:center;
height:50px;
/*line-height:35px; 和 height都可以表示文字在表格中垂直居中*/
}
tr:hover{
background:#cccccc;
}
</style>
</head>
<body>
<!--放在table中的属性width="500" border="1" bgcolor="#b4b4b4" align="center" style="border-collapse:collapse;" cellspacing="0" cellpadding="0"-->
<table >
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>数学</th>
<th>语文</th>
</tr>
</thead>
<tbody>
<tr>
<td>001</td>
<td>张三</td>
<td>92</td>
<td>98</td>
</tr>
<tr>
<td>002</td>
<td>李四</td>
<td>99</td>
<td>88</td>
</tr>
</tbody>
</table>
</body>
</html>
方法2:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>当鼠标经过表格的一行时这一行的背景颜色发生改变方法2</title>
<style>
table, th, tr, td{
width:500px;
border:1px solid red;
border-collapse:collapse;
}
tr, td{
text-align:center;
line-height:50px;
}
</style>
</head>
<body>
<table >
<thead>
<tr οnmοuseοver="this.style.background='gray'" οnmοuseοut="this.style.background='white'">
<th>学号</th>
<th>姓名</th>
<th>数学</th>
<th>语文</th>
</tr>
</thead>
<tbody>
<tr οnmοuseοver="this.style.background='gray'" οnmοuseοut="this.style.background='white'">
<td>001</td>
<td>张三</td>
<td>92</td>
<td>98</td>
</tr>
<tr οnmοuseοver="this.style.background='gray'" οnmοuseοut="this.style.background='white'">
<td>002</td>
<td>李四</td>
<td>99</td>
<td>88</td>
</tr>
<tr οnmοuseοver="this.style.background='gray'" οnmοuseοut="this.style.background='white'">
<td>003</td>
<td>张华</td>
<td>92</td>
<td>90</td>
</tr>
</tbody>
</table>
</body>
</html>