zoukankan
html css js c++ java
表格行高亮显示实现为一个 js 类
<
HTML
>
<
HEAD
>
<
TITLE
></
TITLE
>
<
script language
=
"
javascript
"
>
//
/////////////////////////////////////////////////////////////////////////////
//
//
功能:这个类使得被附加的表格可以支持行点击高亮
//
参数:
//
tbl: 要附加样式的 table.
//
selectedRowIndex: 初始高亮的行的索引(从 0 开始). 此参数可省。
//
hilightColor: 高亮颜色。可省(默认为绿色)
//
//
Author: Neil Chen
//
Date: 2005-09-05
//
//
/////////////////////////////////////////////////////////////////////////////
function
TableRowHilighter(tbl, selectedRowIndex, hilightColor) {
this
.currentRow
=
null
;
this
.hilightColor
=
hilightColor
?
hilightColor : 'green';
if
(selectedRowIndex
!=
null
&&
selectedRowIndex
>=
0
&&
selectedRowIndex
<
tbl.rows.length)
{
this
.currentRow
=
tbl.rows[selectedRowIndex];
tbl.rows[selectedRowIndex].runtimeStyle.backgroundColor
=
this
.hilightColor;
}
var
_this
=
this
;
tbl.attachEvent(
"
onclick
"
, table_onclick);
function
table_onclick() {
var
e
=
event.srcElement;
if
(e.tagName
==
'TD')
e
=
e.parentElement;
if
(e.tagName
!=
'TR')
return
;
if
(e
==
_this.currentRow)
return
;
if
(_this.currentRow)
_this.currentRow.runtimeStyle.backgroundColor
=
'';
e.runtimeStyle.backgroundColor
=
_this.hilightColor;
_this.currentRow
=
e;
}
}
</
script
>
</
HEAD
>
<
BODY
>
<
table id
=
"
table1
"
border
=
"
1
"
cellspacing
=
"
0
"
cellpadding
=
"
5
"
>
<
tr
>
<
td onclick
=
"
alert('a');
"
>
a
</
td
>
<
td
>
b
</
td
>
<
td
>
c
</
td
>
</
tr
>
<
tr
>
<
td
>
d
</
td
>
<
td
>
e
</
td
>
<
td
>
f
</
td
>
</
tr
>
<
tr
>
<
td
>
g
</
td
>
<
td
>
h
</
td
>
<
td
>
i
</
td
>
</
tr
>
</
table
>
<
br
>
<
table id
=
"
table2
"
border
=
"
1
"
cellspacing
=
"
0
"
cellpadding
=
"
5
"
>
<
tr
>
<
td
>
a
</
td
>
<
td
>
b
</
td
>
<
td
>
c
</
td
>
</
tr
>
<
tr
>
<
td
>
d
</
td
>
<
td
>
e
</
td
>
<
td
>
f
</
td
>
</
tr
>
<
tr
>
<
td
>
g
</
td
>
<
td
>
h
</
td
>
<
td
>
i
</
td
>
</
tr
>
</
table
>
<
br
>
<
table id
=
"
table3
"
border
=
"
1
"
cellspacing
=
"
0
"
cellpadding
=
"
5
"
>
<
tr
>
<
td
>
a
</
td
>
<
td
>
b
</
td
>
<
td
>
c
</
td
>
</
tr
>
<
tr
>
<
td
>
d
</
td
>
<
td
>
e
</
td
>
<
td
>
f
</
td
>
</
tr
>
<
tr
>
<
td
>
g
</
td
>
<
td
>
h
</
td
>
<
td
>
i
</
td
>
</
tr
>
</
table
>
</
BODY
>
<
script
>
//
调用范例
var
hilighter1
=
new
TableRowHilighter(document.getElementById('table1'),
1
, 'yellow');
var
hilighter2
=
new
TableRowHilighter(document.getElementById('table2'),
0
, 'lightsteelblue');
var
hilighter3
=
new
TableRowHilighter(document.getElementById('table3'),
2
);
</
script
>
</
HTML
>
其中行点击的事件处理用的是 attachEvent 方法,因此不支持 IE 5.5 以下的浏览器,以及非 IE 浏览器。但有一个好处就是该高亮功能不影响 HTML 元素中原有的 onclick 事件处理逻辑。
查看全文
相关阅读:
毕业设计每日博客——第八周4
毕业设计每日博客——第八周3
毕业设计每日博客——第八周2
毕业设计每日博客——第八周1
毕业设计每日博客——第七周5
毕业设计每日博客——第七周4
毕业设计每日博客——第七周3
毕业设计每日博客——第七周2
毕业设计每日博客——第七周1
毕业设计每日博客——第六周5
原文地址:https://www.cnblogs.com/RChen/p/230567.html
最新文章
S3C2440看门狗定时器原理
c#中接口、抽象类、继承综合小练习
c#和Java中的多态
c#和Java中的接口
c#和Java中的抽象类
c#和Java中的继承
Java中的Static修饰符
Java中this关键字的用法
控制台设置临时环境变量
jquery中的$.fn的用法
热门文章
Google Chrome快捷键
SpringMVC 常用注解都有哪些?
Typora侧边栏的大纲视图折叠( win系统 )
MyBatis中通用Mapper接口以及Example的方法解析
抽象类和接口有什么不同点?
博文编写模板
面试题:说下原生jdbc 操作数据库流程?
跨域问题&cors解决跨域
CollectionUtils工具类使用指南
通用Mapper的select(T t)的使用
Copyright © 2011-2022 走看看