利用jQuery实现表格的隔行变色、高亮显示
更新:HHH   时间:2023-1-7


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
table{
text-align: center;
font-weight: bold;
}
tr{
height: 32px;
}
</style>
<script src="jquery-1.8.3.js"></script>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" width="400">
<tr>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<tr>
<td>1</td>
<td>张三</td>
<td>18</td>
</tr>
<tr>
<td>2</td>
<td>李四</td>
<td>19</td>
</tr>
<tr>
<td>3</td>
<td>王五</td>
<td>20</td>
</tr>
<tr>
<td>4</td>
<td>赵六</td>
<td>21</td>
</tr>
<tr>
<td>5</td>
<td>钱七</td>
<td>22</td>
</tr>
</table>
</body>
<script type="text/javascript">
// 设置表头的颜色 表头不需要高亮显示
$('table tr:first').css({'background':'#694d9f'});
$('table tr:even').not('table tr:first').css({'background':'#888'});
$('table tr:odd').css({'background':'#ccc'});
// 表头是不需要高亮显示的
$('table tr').not('table tr:first').hover(
function(){
color=$(this).css('backgroundColor'); // 获取之前的背景颜色
$(this).css({'background':'yellow'});
},
function(){
$(this).css({'background':color});
}
);
</script>
</html>

返回web开发教程...