分享

“鼠标移至表格中某一行时,此行高亮显示”通用解决方案

 ylzrx 2011-12-30
鼠标移至表格中某一行时,此行高亮显示。用Javascript实现的。ASP.NET和JAVA平台都能使用,比较方便。试验了半天。如下代码:

<html>
<head>
<title>鼠标移至表格中某一行时,此行高亮显示</title>

</head>
<body>
<table id="tb1">
<tr>
<td>a</td><td>b</td><td>c</td>
</tr>
<tr>
<td>a</td><td>b</td><td>c</td>
</tr>
<tr>
<td>a</td><td>b</td><td>c</td>
</tr>
<tr>
<td>a</td><td>b</td><td>c</td>
</tr>
<tr >
<td>a</td><td>b</td><td>c</td>
</tr>
</table>

<script>
var c;
     var table1 = document.getElementById("tb1");
     var rows =table1.getElementsByTagName('tr');        
     alert(rows.length);
     for(var i=0;i<rows.length;i++)
{
     var row = rows[i];
     row. = function() {c=this.style.backgroundColor;this.style.backgroundColor='red';}
     row. = function(){this.style.backgroundColor=c;}
}    
</script>
</body>
</html>
 
 
可能有些朋友要用这种写法:
row.setAttribute("onMouseOver","c=this.style.backgroundColor;this.style.backgroundColor='red'");
但是微软的IE对DOM的setAttribute方法支持不好,用这种写法写了以后没效果,可能在FireFox和谷歌浏览器中会成功。我刚刚试验了半天。那个郁闷哦...还好最后查到用这种写法成功了。大家可以参考一下:
row. = function() {c=this.style.backgroundColor;this.style.backgroundColor='red';}
其中c这个变量时用来存放原始的backgroundColor,最后onmouseout的时候,要用c这个变量里面的值还原初始的backgroundColor。
 
转载:

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约