DIV CSS 佈局教程網

用 jQuery 的 hover 讓滑鼠移過表格列時背景變色
編輯:JQuery特效代碼     

讓表格在滑鼠移過每一列時自動高亮背景色,是提升可讀性的常見手法。這只要用 jQuery 的 hover 方法,進出時分別加上與移除一個 class 就能完成。

完整範例:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>StripingTable</title>
<script src="jquery-1.1.3.pack.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#orderedlist tbody tr").hover(function() {
$(this).addClass("blue");
}, function() {
$(this).removeClass("blue");
});
});
</script>
<style>
.blue { background:#bcd4ec; }
</style>
</head>
<body>
<table id="orderedlist" width="50%" border="0" cellspacing="0" cellpadding="0">
<thead>
<tr><th>姓名</th><th>年齡</th><th>QQ</th><th>Email</th></tr>
</thead>
<tbody>
<tr><td>鄧國梁</td><td>23</td><td>31540205</td><td>gl.deng@gmail.com</td></tr>
……
</tbody>
</table>
</body>
</html>

效果如下:

原理很單純:hover 接受兩個函數,第一個在滑鼠移入時執行,替目前這列加上 blue 這個 class;第二個在滑鼠移出時執行,把 class 移除。真正負責變色的其實是 CSS 裡的 .blue 規則,jQuery 只是動態地幫元素掛上或取下這個 class,職責切分得很清楚。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved