DIV CSS 佈局教程網

滑鼠移經表格列時動態變換背景色的實作
編輯:JavaScript綜合知識     

為表格加入滑鼠移入高亮效果,能提升資料瀏覽的友善度。本文示範當滑鼠移經某個 tr 時,將該列背景變色,同時清除其他列的高亮。

程式碼如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>鼠標經過給tr換顏色</title>
</head>
<body>
<table align="center" width="100%" border="1" cellspacing="1" cellpadding="1" >
 <tr style="cursor:hand " onmousemove="changeTrColor(this)">
  <td align="center">1</td>
  <td height="20"> 123</td>
  <td height="20"> abvx</td>
  <td height="20"> 465465</td>
  <td height="20">546654654</td>
 </tr>
 <tr style="cursor:hand " onmousemove="changeTrColor(this)">
  <td align="center">1</td>
  <td height="20"> 123</td>
  <td height="20"> abvx</td>
  <td height="20"> 465465</td>
  <td height="20">546654654</td>
 </tr>
</table>
<script type="text/javascript">
function changeTrColor(obj){
  var _table=obj.parentNode;
  for (var i=0;i<_table.rows.length;i++){
    _table.rows[i].style.backgroundColor="";
  }
  obj.style.backgroundColor="blue";
}
</script>
</body>
</html>

changeTrColor 會先取得目前的表格物件,將所有列的背景重設為空,再將觸發事件的該列設為藍色。這樣就能確保任何時刻只有滑鼠所在的列被高亮,達到乾淨俐落的視覺效果。

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