爱程序网

jQuery实现的table表格隔行换色代码实例

来源: 阅读:

jQuery实现的table表格隔行换色代码实例:
下面是一段代码实例,能够实现隔行变色的效果,这是网站人性化措施之一,在实际应用中的使用非常广泛。
代码如下:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<meta name="author" content="http://www.softwhy.com/" /> 
<title>table细线表格-蚂蚁部落</title> 
<style type="text/css">  
#thetable{ 
  background-color:green; 
  font-size:12px; 
} 
#thetable th{ 
  text-align:center; 
  background-color:#CCF; 
  height:30px; 
  line-height:30px; 
} 
#thetable td{ 
  width:150px; 
  height:30px; 
  line-height:30px; 
  text-align:center; 
} 
.even{background:#FFF38F;} 
.odd{background:#FFFFEE;} 
.selected{background:#FF9900;}
</style>  
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script> 
<script type="text/javascript"> 
$(function(){ 
  $("#thetable tr:odd").addClass("odd"); 
  $("#thetable tr:even").addClass("even"); 
}); 
</script> 
</head>  
<body>  
<table cellpadding="0" cellspacing="1" id="thetable"> 
  <tr> 
    <th>标题一</th> 
    <th>标题二</th> 
    <th>标题三</th> 
    <th>标题四</th> 
  </tr> 
  <tr> 
    <td>蚂蚁部落一</td> 
    <td>蚂蚁部落二</td> 
    <td>蚂蚁部落三</td> 
    <td>蚂蚁部落四</td> 
  </tr> 
  <tr> 
    <td>蚂蚁部落一</td> 
    <td>蚂蚁部落二</td> 
    <td>蚂蚁部落三</td> 
    <td>蚂蚁部落四</td> 
  </tr> 
  <tr> 
    <td>蚂蚁部落一</td> 
    <td>蚂蚁部落二</td> 
    <td>蚂蚁部落三</td> 
    <td>蚂蚁部落四</td> 
  </tr> 
  <tr> 
    <td>蚂蚁部落一</td> 
    <td>蚂蚁部落二</td> 
    <td>蚂蚁部落三</td> 
    <td>蚂蚁部落四</td> 
  </tr> 
  <tr> 
    <td>蚂蚁部落一</td> 
    <td>蚂蚁部落二</td> 
    <td>蚂蚁部落三</td> 
    <td>蚂蚁部落四</td> 
  </tr> 
</table> 
</body>  
</html>

上面的代码实现了隔行变色的效果,代码比较简单这里就不多介绍了,可以参阅相关阅读。
相关阅读:
1.: odd选择器可以参阅jQuery的: odd选择器一章节。
2.addClass()函数可以参阅jQuery的addClass()方法一章节。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=14211

更多内容可以参阅:http://www.softwhy.com/jquery/

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助