表格隔行显示不同背景颜色,表格奇数行和偶数行交替显示不同的背景颜色。
一、纯CSS3实现表格隔行变色
关键CSS代码介绍
table tr:nth-child(odd){background:#F4F4F4;} table td:nth-child(even){color:#C00;} table tr:nth-child(5){background:#73B1E0;color:#FFF;}
二、jquery实现表格奇数行和偶数行交替显示不同的背景颜色
<html> <head> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $("tr:even").css("background","#090"); $("tr:odd").css("background","red"); }) </script> </head> <body> <table width="400" > <tr> <td>www.125jz.com</td> </tr> <tr> <td>网页设计教程</td> </tr> <tr> <td>www.125jz.com</td> </tr> <tr> <td>网页设计教程</td> </tr> </table> </body> </html>
三、jquery实现表格隔行变色、鼠标悬停高亮
<!doctype html> <html><head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>表格隔行变色_CSS实现鼠标悬停高亮</title> <meta name="keywords" content="" /> <meta name="description" content="" /> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript"> $(function(){ //通过jQuery控制表格隔行换色,并鼠标悬停变色 $('tr:even:gt(0)').addClass('tr_even'); //默认偶数行背景色,无视标题行用:gt(0) $('tr:odd').addClass('tr_odd'); //默认奇数数行背景色 }); </script> <style type="text/css"> .headCls{background-color:#ccc;} /* 标题背景色 */ .tr_even{background-color:#EBF8FF} /* 偶数行背景色 */ .tr_odd{background-color:#DDECF3} /* 奇数行背景色 */ .tr_even:hover,.tr_odd:hover{background-color:red;} /* 通过CSS实现鼠标悬停高亮色 */ </style> </head> <body> <table border="1" width="100%" cellspacing="0" cellpadding="0"> <tr class="headCls"> <th>CSS实现鼠标悬停高亮</th> <th>CSS实现鼠标悬停高亮</th> </tr> <tr> <td>data1_1</td> <td>data1_2</td> </tr> <tr> <td>data2_1</td> <td>data2_2</td> </tr> <tr> <td>data3_1</td> <td>data3_2</td> </tr> <tr> <td>data4_1</td> <td>data4_2</td> </tr> <tr> <td>data5_1</td> <td>data5_2</td> </tr> </table> </body> </html>
125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/2483.html