表格行高亮显示实现为一个 js 类

此博客为转载内容,转载自https://www.cnblogs.com/RChen/archive/2005/09/05/230567.html ,涉及JavaScript相关知识。
None.gif<HTML>
None.gif
<HEAD>
None.gif
<TITLE></TITLE>
None.gif
None.gif
<script language="javascript">
None.gif
///////////////////////////////////////////////////////////////////////////////
None.gif//
None.gif//
 功能:这个类使得被附加的表格可以支持行点击高亮
None.gif//
 参数:
None.gif//
            tbl:                要附加样式的 table.
None.gif//
            selectedRowIndex:    初始高亮的行的索引(从 0 开始). 此参数可省。
None.gif//
            hilightColor:        高亮颜色。可省(默认为绿色)
None.gif//
None.gif//
 Author:    Neil Chen
None.gif//
 Date:    2005-09-05
None.gif//
None.gif//
/////////////////////////////////////////////////////////////////////////////
None.gif
function TableRowHilighter(tbl, selectedRowIndex, hilightColor) {
None.gif    
this.currentRow = null;
None.gif    
this.hilightColor = hilightColor ? hilightColor : 'green';    
None.gif
None.gif    
if (selectedRowIndex != null 
None.gif        
&& selectedRowIndex >= 0 
None.gif        
&& selectedRowIndex < tbl.rows.length) 
None.gif    {
None.gif        
this.currentRow = tbl.rows[selectedRowIndex];        
None.gif        tbl.rows[selectedRowIndex].runtimeStyle.backgroundColor 
= this.hilightColor;
None.gif    }
None.gif
None.gif    
var _this = this;
None.gif    tbl.attachEvent(
"onclick", table_onclick);    
None.gif
None.gif    
function table_onclick() {
None.gif        
var e = event.srcElement;        
None.gif        
if (e.tagName == 'TD')
None.gif            e 
= e.parentElement;            
None.gif        
if (e.tagName != 'TR') return;
None.gif        
if (e == _this.currentRow) return;        
None.gif        
if (_this.currentRow)
None.gif            _this.currentRow.runtimeStyle.backgroundColor 
= '';
None.gif            
None.gif        e.runtimeStyle.backgroundColor 
= _this.hilightColor;
None.gif        _this.currentRow 
= e;
None.gif    }
None.gif}
None.gif
None.gif
None.gif
</script>
None.gif
None.gif
</HEAD>
None.gif
<BODY>
None.gif
None.gif
<table id="table1" border="1" cellspacing="0" cellpadding="5">
None.gif    
<tr>
None.gif        
<td onclick="alert('a');">a</td>
None.gif        
<td>b</td>
None.gif        
<td>c</td>
None.gif    
</tr>
None.gif    
<tr>
None.gif        
<td>d</td>
None.gif        
<td>e</td>
None.gif        
<td>f</td>
None.gif    
</tr>
None.gif    
<tr>
None.gif        
<td>g</td>
None.gif        
<td>h</td>
None.gif        
<td>i</td>
None.gif    
</tr>
None.gif
</table>
None.gif
None.gif
<br>
None.gif
<table id="table2" border="1" cellspacing="0" cellpadding="5">
None.gif    
<tr>
None.gif        
<td>a</td>
None.gif        
<td>b</td>
None.gif        
<td>c</td>
None.gif    
</tr>
None.gif    
<tr>
None.gif        
<td>d</td>
None.gif        
<td>e</td>
None.gif        
<td>f</td>
None.gif    
</tr>
None.gif    
<tr>
None.gif        
<td>g</td>
None.gif        
<td>h</td>
None.gif        
<td>i</td>
None.gif    
</tr>
None.gif
</table>
None.gif
None.gif
<br>
None.gif
<table id="table3" border="1" cellspacing="0" cellpadding="5">
None.gif    
<tr>
None.gif        
<td>a</td>
None.gif        
<td>b</td>
None.gif        
<td>c</td>
None.gif    
</tr>
None.gif    
<tr>
None.gif        
<td>d</td>
None.gif        
<td>e</td>
None.gif        
<td>f</td>
None.gif    
</tr>
None.gif    
<tr>
None.gif        
<td>g</td>
None.gif        
<td>h</td>
None.gif        
<td>i</td>
None.gif    
</tr>
None.gif
</table>
None.gif
None.gif
</BODY>
None.gif
None.gif
<script>
None.gif
// 调用范例
None.gif
var hilighter1 = new TableRowHilighter(document.getElementById('table1'), 1, 'yellow');
None.gif
var hilighter2 = new TableRowHilighter(document.getElementById('table2'), 0, 'lightsteelblue');
None.gif
var hilighter3 = new TableRowHilighter(document.getElementById('table3'), 2);
None.gif
</script>
None.gif
None.gif
</HTML>
None.gif

其中行点击的事件处理用的是 attachEvent 方法,因此不支持 IE 5.5 以下的浏览器,以及非 IE 浏览器。但有一个好处就是该高亮功能不影响 HTML 元素中原有的 onclick 事件处理逻辑。

转载于:https://www.cnblogs.com/RChen/archive/2005/09/05/230567.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值