比較高效的表格行背景變色及選定高亮JS

來源:互聯網
上載者:User
比較高效的表格行背景變色及選定高亮JS下面這個例一摘錄自:
http://blog.breakn.net/article.asp?id=447
例一:
//點擊當前選中行的時候設定當前行的顏色,同時恢複除當前行外的行的顏色及滑鼠事件 
function selectRow(target) 

var sTable = document.getElementById("ServiceListTable") 
for(var i=1;i<sTable.rows.length;i++) //遍曆除第一行外的所有行 

if (sTable.rows[i] != target) //判斷是否當前選定行 

sTable.rows[i].bgColor = "#ffffff"; //設定背景色 
sTable.rows[i].onmouseover = resumeRowOver; //增加onmouseover 事件 
sTable.rows[i].onmouseout = resumeRowOut;//增加onmouseout 事件 

else 

sTable.rows[i].bgColor = "#d3d3d3"; 
sTable.rows[i].onmouseover = ""; //去除滑鼠事件 
sTable.rows[i].onmouseout = ""; //去除滑鼠事件 



//移過時tr的背景色 
function rowOver(target) 

target.bgColor='#e4e4e4'; 

//移出時tr的背景色 
function rowOut(target) 

target.bgColor='#ffffff'; 

//恢複tr的的onmouseover事件配套調用函數 
function resumeRowOver() 

rowOver(this); 

//恢複tr的的onmouseout事件配套調用函數 
function resumeRowOut() 

rowOut(this); 
}   頁面測試表格: <table width="100%" border="0" cellspacing="0" cellpadding="0" id="ServiceListTable"> 
<tr> 
<th>服務事項</th> 
<th>N</th> 
<th>狀態</th> 
<th>辦結</th> 
<th>資料</th> 
</tr> 
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)"> 
<td>相關內容</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
</tr> 
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)"> 
<td>相關內容</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
</tr> 
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)"> 
<td>相關內容</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
</tr> 
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)"> 
<td>相關內容</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
</tr> 
</table>    例二:
上述為Row中添加效果,做適當修改,為每個Cell添加效果: <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test</title>
<script language="javascript">
 function selectCell(target) { 
  var sTable = document.getElementById("table1") 
  for(var i=0;i<sTable.rows.length;i++) {   
   for(var j=0; j<sTable.rows[i].cells.length; j++) {
    if (sTable.rows[i].cells[j] != target) {
     sTable.rows[i].cells[j].bgColor = "#ffffff";  
     sTable.rows[i].cells[j].onmouseover = resumeCellOver;  
     sTable.rows[i].cells[j].onmouseout = resumeCellOut;
    }
    else {
     sTable.rows[i].cells[j].bgColor = "#d3d3d3"; 
     sTable.rows[i].cells[j].onmouseover = "";
     sTable.rows[i].cells[j].onmouseout = "";
    }  
   }
  } 
 }
 
 function cellOver(target) { 
  target.bgColor='#e4e4e4'; 
 } 
 
 function cellOut(target) { 
  target.bgColor='#ffffff'; 
 } 
 
 function resumeCellOver() { 
  cellOver(this); 
 } 
 
 function resumeCellOut() { 
  cellOut(this); 
 } 
</script>
</head>
<body>
 <table align="center" width="200" height="200" border="1" cellspacing="0" cellpadding="0" id="table1"> 
  <tr> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">xxx</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">xxx</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">xxx</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">xxx</td> 
  </tr> 
  <tr > 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">&nbsp;</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">&nbsp;</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">&nbsp;</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">&nbsp;</td> 
  </tr> 
  <tr>   
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">xxx</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">xxx</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">xxx</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">xxx</td> 
  </tr> 
  <tr> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">&nbsp;</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">&nbsp;</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">&nbsp;</td> 
   <td align="center" onmouseover="cellOver(this)" onmouseout="cellOut(this)" onclick="selectCell(this)">&nbsp;</td> 
  </tr> 
 </table> 
</body>
</html>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.