网络常用的表格形式是细线表格,但是对于内容较多的显示,则容易造成视觉疲劳,较好的解决方法除了减淡表格线条颜色,增大线条和字色的对比,增大表格内tr和td属性中的padding值之外,运用无线条交替背景的表格则可以大大增强可视性。
方法一:非常简单,用expression法设置table中tr的属性:
#table tr{
background-color:expression('#F0F0F0,#DDD'.split(',')[rowIndex%2]);
}
注意:这里用的expression就是CSS+JS的结合物,归根到底还是应用了js效果,但JS与firefox的兼容性有限,这种效果在firefox下无法显示出来。
此外,expression法如果结合调节height,padding,则可以得到视觉效果极佳的表格。
方法二:expression实现鼠标移动上去,表格背景变色
具体原理同上,代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<style>
body{}
.testGrid{border-collapse: collapse;background:WhiteSmoke;}
.selected{background:Burlywood;color:#fff;}
.over{background:Bisque;}
.testGrid td{border:#f4a460 1px solid;padding:5px;}
.testGrid tr{font-size:12px;
obj: expression(onclick=function (){if(this.className=='selected'){this.className='';}else{this.className='selected'};for(var i=0;i<this.parentNode.childNodes.length;i++){if(this.parentNode.childNodes[i]!=this){this.parentNode.childNodes[i].className='';}}},onmouseover=function (){if(this.className!="selected"){this.className="over";this.title='单击选中此项'}else{this.title='单击取消选中'}},onmouseout=function (){if(this.className!="selected"){this.className="";}});
}
</style>
</head>
<body>
<table width="80%" class="testGrid" cellpadding="0" cellspacing="0">
<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>
方法三:交替使用背景图片,还是expression
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<style>
body{}
.testGrid{border-collapse: collapse;background:WhiteSmoke;}
.selected{background:Burlywood;color:#fff;}
.over{background:Bisque;}
.testGrid td{border:#f4a460 1px solid;padding:5px;padding:30px 0 0 0;height:70px;text-align:center;}
.testGrid tr{font-size:12px;
background:expression('url(http://www.zj-blog.com/temp/images/f'+(rowIndex%3+1)+'.gif) top center no-repeat');
}
</style>
</head>
<body>
<table width="70px" class="testGrid" cellpadding="0" cellspacing="0">
<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>
方法四:JS,唯一可兼容IE6的方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>关于表格的间隔变色</title>
</head>
<body>
<table width="100%" id="stockpost1" summary="列表1">
<thead> <tr>
<th scope="col" class="stockpostw1">列表1</th>
</tr>
<tr> <tr> <td> </td> </tr>
<tr> <td> </td> </tr>
<tr> <td> </td> </tr>
</thead>
</table>
<table width="100%" id="stockpost2" summary="列表2">
<thead> <tr>
<th scope="col">列表2</th> </tr>
<tr> </tr>
<tr> <td> </td> </tr>
<tr> <td> </td> </tr>
<tr> <td> </td> </tr>
</thead> </table>
<table width="100%" id="stockpost3" summary="列表3">
<thead> <tr>
<th scope="col">列表3</th> </tr>
<tr> </tr> <tr> <td> </td> </tr>
<tr> <td> </td> </tr>
<tr> <td> </td> </tr>
</thead> </table>
<script language="javascript" >
function classcolor(classid,color) {_ObjTable=document.getElementById(classid); for(var i=2; i<_ObjTable.rows.length; i=i+2){ _ObjTable.rows[i].bgColor=color; } }</script> <script language="javascript" >classcolor("stockpost1","#66ff33")</script> <script language="javascript" >classcolor("stockpost2","#009933")</script> <script language="javascript" >classcolor("stockpost3","#EDF7F7")
</script>
</body>
</html>
Comments