高效的表格行背景隔行變色及選定高亮的JS代碼
更新時(shí)間:2010年12月04日 12:41:47 作者:
一個(gè)項(xiàng)目要用,又不想用jquery之類的東東。先去網(wǎng)上搜索了下,找到了不少在CSS中執(zhí)行JS的表格行變色方式,不過這類方式在表格行多的時(shí)候相當(dāng)卡,在IE7和firefox3中測試正常。
這段JS放在head中
//點(diǎn)擊當(dāng)前選中行的時(shí)候設(shè)置當(dāng)前行的顏色,同時(shí)恢復(fù)除當(dāng)前行外的行的顏色及鼠標(biāo)事件
function selectRow(target)
{
var sTable = document.getElementById("ServiceListTable")
for(var i=1;i<sTable.rows.length;i++) //遍歷除第一行外的所有行
{
if (sTable.rows[i] != target) //判斷是否當(dāng)前選定行
{
sTable.rows[i].bgColor = "#ffffff"; //設(shè)置背景色
sTable.rows[i].onmouseover = resumeRowOver; //增加onmouseover 事件
sTable.rows[i].onmouseout = resumeRowOut;//增加onmouseout 事件
}
else
{
sTable.rows[i].bgColor = "#d3d3d3";
sTable.rows[i].onmouseover = ""; //去除鼠標(biāo)事件
sTable.rows[i].onmouseout = ""; //去除鼠標(biāo)事件
}
}
}
//移過時(shí)tr的背景色
function rowOver(target)
{
target.bgColor='#e4e4e4';
}
//移出時(shí)tr的背景色
function rowOut(target)
{
target.bgColor='#ffffff';
}
//恢復(fù)tr的的onmouseover事件配套調(diào)用函數(shù)
function resumeRowOver()
{
rowOver(this);
}
//恢復(fù)tr的的onmouseout事件配套調(diào)用函數(shù)
function resumeRowOut()
{
rowOut(this);
}
關(guān)于最后兩個(gè)函數(shù)resumeRowOver和resumeRowOut為什么這樣寫參考我之前寫的通過js給頁面元素添加事件
對應(yīng)的表格HTML
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="ServiceListTable">
<tr>
<th>服務(wù)事項(xiàng)</th>
<th>N</th>
<th>狀態(tài)</th>
<th>辦結(jié)</th>
<th>資料</th>
</tr>
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)">
<td>相關(guān)內(nèi)容</td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)">
<td>相關(guān)內(nèi)容</td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)">
<td>相關(guān)內(nèi)容</td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)">
<td>相關(guān)內(nèi)容</td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
</table>
復(fù)制代碼 代碼如下:
//點(diǎn)擊當(dāng)前選中行的時(shí)候設(shè)置當(dāng)前行的顏色,同時(shí)恢復(fù)除當(dāng)前行外的行的顏色及鼠標(biāo)事件
function selectRow(target)
{
var sTable = document.getElementById("ServiceListTable")
for(var i=1;i<sTable.rows.length;i++) //遍歷除第一行外的所有行
{
if (sTable.rows[i] != target) //判斷是否當(dāng)前選定行
{
sTable.rows[i].bgColor = "#ffffff"; //設(shè)置背景色
sTable.rows[i].onmouseover = resumeRowOver; //增加onmouseover 事件
sTable.rows[i].onmouseout = resumeRowOut;//增加onmouseout 事件
}
else
{
sTable.rows[i].bgColor = "#d3d3d3";
sTable.rows[i].onmouseover = ""; //去除鼠標(biāo)事件
sTable.rows[i].onmouseout = ""; //去除鼠標(biāo)事件
}
}
}
//移過時(shí)tr的背景色
function rowOver(target)
{
target.bgColor='#e4e4e4';
}
//移出時(shí)tr的背景色
function rowOut(target)
{
target.bgColor='#ffffff';
}
//恢復(fù)tr的的onmouseover事件配套調(diào)用函數(shù)
function resumeRowOver()
{
rowOver(this);
}
//恢復(fù)tr的的onmouseout事件配套調(diào)用函數(shù)
function resumeRowOut()
{
rowOut(this);
}
關(guān)于最后兩個(gè)函數(shù)resumeRowOver和resumeRowOut為什么這樣寫參考我之前寫的通過js給頁面元素添加事件
對應(yīng)的表格HTML
復(fù)制代碼 代碼如下:
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="ServiceListTable">
<tr>
<th>服務(wù)事項(xiàng)</th>
<th>N</th>
<th>狀態(tài)</th>
<th>辦結(jié)</th>
<th>資料</th>
</tr>
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)">
<td>相關(guān)內(nèi)容</td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)">
<td>相關(guān)內(nèi)容</td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)">
<td>相關(guān)內(nèi)容</td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr onmouseover="rowOver(this)" onmouseout="rowOut(this)" onclick="selectRow(this)">
<td>相關(guān)內(nèi)容</td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
</table>
您可能感興趣的文章:
- JS控制表格隔行變色
- javascript實(shí)現(xiàn)table表格隔行變色的方法
- js實(shí)現(xiàn)鼠標(biāo)經(jīng)過表格行變色的方法
- 原生JS操作網(wǎng)頁給p元素添加onclick事件及表格隔行變色
- 一個(gè)簡單但常用的javascript表格樣式_鼠標(biāo)劃過行變色 簡潔實(shí)現(xiàn)
- javascript基于jQuery的表格懸停變色/恢復(fù),表格點(diǎn)擊變色/恢復(fù),點(diǎn)擊行選Checkbox
- 用JS控制表格的逐行變色的表單
- javascript表格隔行變色加鼠標(biāo)移入移出及點(diǎn)擊效果的方法
- js兼容標(biāo)準(zhǔn)的表格變色效果
- JS實(shí)現(xiàn)表格隔行變色
相關(guān)文章
js es6系列教程 - 基于new.target屬性與es5改造es6的類語法
下面小編就為大家?guī)硪黄猨s es6系列教程 - 基于new.target屬性與es5改造es6的類語法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
利用svg實(shí)現(xiàn)帶加載進(jìn)度的loading
svg是基于XML,由World?Wide?Web?Consortium?(W3C)聯(lián)盟開發(fā)的一種開放標(biāo)準(zhǔn)的矢量圖形語言,可讓你設(shè)計(jì)激動人心的、高分辨率的Web圖形頁面。本文將使用svg實(shí)現(xiàn)一個(gè)帶加載進(jìn)度的loading,需要的可以參考一下2022-11-11
js利用iframe實(shí)現(xiàn)選項(xiàng)卡效果
這篇文章主要為大家詳細(xì)介紹了js利用iframe實(shí)現(xiàn)選項(xiàng)卡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
JavaScript實(shí)例--創(chuàng)建一個(gè)歡迎cookie
這篇文章主要介紹了JavaScript實(shí)例--創(chuàng)建一個(gè)歡迎cookie,2022-01-01
javascript 面向?qū)ο蠹夹g(shù)基礎(chǔ)教程
看了很多介紹javascript面向?qū)ο蠹夹g(shù)的文章,很暈.為什么?不是因?yàn)閷懙貌缓?而是因?yàn)樘願W.2009-12-12
ES6新特性之類(Class)和繼承(Extends)相關(guān)概念與用法分析
這篇文章主要介紹了ES6新特性之類(Class)和繼承(Extends)相關(guān)概念與用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了ES6中類(Class)和繼承(Extends)的基本概念、語法、使用方法與注意事項(xiàng),需要的朋友可以參考下2017-05-05

