工作学习中经常会用到的代码,很实用
既可以实现隔行换色,又可以实现鼠标经过换色,鼠标移开换为原色
注:如果点击“运行代码”后没有隔行换色效果,刷新页面后可以看到效果,有点小郁闷~~~~~
应用的时候绝对没问题。

<style type="text/css" media="screen">...
table {...}{border-collapse:collapse;border:solid #999;border-width:1px 0 0 1px;}
table td {...}{border:solid #999;border-width:0 1px 1px 0;}
.t1 {...}{background-color:#fff;}/**//* 单行的背景色 */
.t2 {...}{background-color:#eee;}/**//* 双行的背景色 */
.t3 {...}{background-color:#ccc;}/**//* 鼠标经过时的背景色 */
</style>
<table width=400 align=center>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
</table>
<script type="text/javascript">...
var Ptr=document.getElementsByTagName("tr");
function recolor() ...{
for (i=1;i<Ptr.length+1;i++) ...{
Ptr[i-1].className = (i%2>0)?"t1":"t2";
}
}
window.onload=recolor;
for(var i=0;i<Ptr.length;i++) ...{
Ptr[i].onmouseover=function()...{
this.tmpClass=this.className;
this.className = "t3";
};
Ptr[i].onmouseout=function()...{
this.className=this.tmpClass;
};
}
</script>
本文介绍了一种使用HTML、CSS和JavaScript实现表格隔行换色及鼠标悬停时改变背景颜色的方法。通过简单的代码实现了美化表格的功能,适用于网页设计与前端开发。
948

被折叠的 条评论
为什么被折叠?



