网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 手机学院 | 邮件系统 | 网络安全 | 认证考试
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网页制作 > Javascript教程
网页制作:Dreamweaver教程,FrontPages教程,Javascript教程,HTML教程,CSS教程,心得技巧,DHTML教程,网页特效,Discuz!论坛
本月文章推荐
.JavaScript方法和技巧大全.
.javascript eval().
.JavaScript面向对象 “四段式”类.
.单行Javascript实现Memoization.
.JavaScript学习笔记(6) 通过延.
.Javascript实例:Select的OnChan.
.javascript操作table.
.FireFox 如何用Javascript 修改状.
.JAVASCRIPT 贪吃蛇.
.判断JavaScript对象是否可用的正.
.突破IE屏蔽限制,自己的网站使劲.
.Email地址加密javascript版.
.用xmlhttp和Java session监听改善.
.125个常用javascript语句.
.javascript 限制输入和粘贴 IE和.
.js文件封装javascript在html中获.
.图片自动缩小的js代码,用以防止图.
.JavaScript[对象.属性]集锦、事件.
.JS+CSS打造可拖动的聊天窗口层.
.网站论坛注册中常用的一种倒记时.

比较高效的表格行背景变色及选定高亮JS

发表日期:2008-12-12

  一个项目要用,又不想用jquery之类的东东。先去网上搜索了下,找到了不少在CSS中执行JS的表格行变色方式,不过这类方式在表格行多的时候相当卡,就自己按着最原始的方式倒腾了个出来献给广大的JS小白们(本人也算上JS小白之一  ),在IE7和firefox3中测试正常。

  这段JS放在head中view plaincopy to clipboardprint?
//点击当前选中行的时候设置当前行的颜色,同时恢复除当前行外的行的颜色及鼠标事件  
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);  

 //点击当前选中行的时候设置当前行的颜色,同时恢复除当前行外的行的颜色及鼠标事件
 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);
 }  关于最后两个函数resumeRowOver和resumeRowOut为什么这样写参考我之前写的通过js给页面元素添加事件
  对应的表格HTMLview plaincopy to clipboardprint?
<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> 
from:http://blog.breakn.net/article.asp?id=447

上一篇:现有的Web打印控制技术分成几种方案 人气:1116
下一篇:通过js给页面元素添加事件 人气:667
浏览全部表格行背景变色的内容 Dreamweaver插件下载 网页广告代码 2009年新年快乐