61,112
社区成员
发帖
与我相关
我的任务
分享
<html>
<head>
<script type="text/javascript">
window.onload=function highlightTableRow(){
var tables = document.getElementsByTagName("table");
for(var k=0;k<tables.length;k++){
if(tables[k].getAttribute("highlight")=="yes"){
var rows = tables[k].getElementsByTagName("tr");
var from = 0, to = rows.length;
if(tables[k].getAttribute("from")){
from = Number(tables[k].getAttribute("from"))-1;//从第from行起开始高亮鼠标所在行
}
if(tables[k].getAttribute("to")){
to = rows.length + Number(tables[k].getAttribute("to"))+1;//高亮鼠标所在行一直到倒数第Math.abs(to)行
}
for(var i=from;i<to;i++){
if(i%2==0){
rows[i].style.backgroundColor = tables[k].getAttribute("evenColor");
rows[i].setAttribute("alternativeColor",tables[k].getAttribute("evenColor"));
}else{
rows[i].style.backgroundColor = tables[k].getAttribute("oddColor");
rows[i].setAttribute("alternativeColor",tables[k].getAttribute("oddColor"));
}
rows[i].onmouseover = function(){//第一个parentNode是tbody,第二个才是table
this.style.backgroundColor = this.parentNode.parentNode.getAttribute("highlightColor");
};
rows[i].onmouseout = function(){
this.style.backgroundColor = this.getAttribute("alternativeColor");
};
}
}
}
}
</script>
</head>
<body>
<table border="1" highlight="yes" from="1" to="-1" highlightColor="#0099FF" oddColor="#B6DBE4" evenColor="#F1F2F4">
<tr><td>第一行</td></tr>
<tr><td>第二行</td></tr>
<tr><td>第三行</td></tr>
<tr><td>第四行</td></tr>
<tr><td>第五行</td></tr>
<tr><td>第六行</td></tr>
</table>
</body>
</html>