表格单元格交错着色实现思路及代码

(编辑:jimmy 日期: 2024/10/10 浏览:2)

【Title】[原]表格单元格交错着色
【Abstract】以空间换时间,循环确定所着颜色。
【Environment】jQuery
【Author】wintys (wintys@gmail.com) http://wintys.cnblogs.com
【Content】:
1、效果
表格单元格交错着色实现思路及代码 
2、问题描述
对如下表格中的单元格进行交错着色。表格中HTML标签、内容已定。
复制代码 代码如下:
<div id="tablecontainer" align="center">
<table style="border-collapse:collapse;" cellspacing="0">
<tbody>
<tr>
<td><a href="http://www.yunyun.com/">TR0-TD0</a></td>
<td><a href="http://www.yunyun.com/">TR0-TD1</a></td>
<td><a href="http://www.yunyun.com/">TR0-TD2</a></td>
<td><a href="http://www.yunyun.com/">TR0-TD3</a></td>
</tr>
<tr>
<td><a href="http://www.yunyun.com/">TR1-TD0</a></td>
<td><a href="http://www.yunyun.com/">TR1-TD1</a></td>
<td><a href="http://www.yunyun.com/">TR1-TD2</a></td>
<td><a href="http://www.yunyun.com/">TR1-TD3</a></td>
</tr>
<tr>
<td><a href="http://www.yunyun.com/">TR2-TD0</a></td>
<td><a href="http://www.yunyun.com/">TR2-TD1</a></td>
<td><a href="http://www.yunyun.com/">TR2-TD2</a></td>
<td><a href="http://www.yunyun.com/">TR2-TD3</a></td>
</tr>
<tr>
<td><a href="http://www.yunyun.com/">TR3-TD0</a></td>
<td><a href="http://www.yunyun.com/">TR3-TD1</a></td>
<td><a href="http://www.yunyun.com/">TR3-TD2</a></td>
<td><a href="http://www.yunyun.com/">TR3-TD3</a></td>
</tr>
</tbody>
</table>
</div>

3、实现
3.1、CSS
复制代码 代码如下:
<style type="text/css">
.tableitem0 {
background: none repeat scroll 0 0 #F65314;
color: #FFFFFF;
}
.tableitem1 {
background: none repeat scroll 0 0 #7CBB00;
color: #FFFFFF;
}
.tableitem2 {
background: none repeat scroll 0 0 #00A1F1;
color: #FFFFFF;
}
.tableitem3 {
background: none repeat scroll 0 0 #FFBB00;
color: #FFFFFF;
}
#tablecontainer td {
padding: 5px;
}
.tableitem {
width: 15%;
}
.tableitem a {
display: block;
font-size: 18px;
height: 35px;
margin: 0 auto;
padding: 15px 20px;
text-align: center;
border-bottom:none;
}
.tableitem a:hover, .tableitem a:visited {
color: #FFFFFF !important;
}
.tableitem a:hover, .tableitem a:active{
opacity: 0.8;
}
</style>

3.2、JS代码
复制代码 代码如下:
<script type="text/javascript">
function setTableStyle(){
$("#tablecontainer tr").each(function(i){//获得所有的tr,进行each循环遍历,并对每个进行操作
var tr = $(this);//得到本次循环里的这个tr
setTableItemStyle(tr,i%4);//每行四个单元格,四种颜色循环交错着色。
});
}
function setTableItemStyle(tr,base){
//【重点】:以空间换时间,循环确定所着颜色。
var tableitem = ["tableitem0","tableitem1","tableitem2","tableitem3","tableitem0","tableitem1","tableitem2"];
for(var i = 0; i < 4;i ++){
var td = tr.children("td").eq(i);
var td_a = td.find("a");
td.addClass("tableitem");
//【重点】:base确定起始颜色,i确定本次需要着色的单元格。
td_a.addClass(tableitem[base + i]);
}
}
$(function(){
setTableStyle();
});
</script>

一句话新闻

高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。