网上很多关于表格隔行变色的方法,其实很多都是只能在IE内核浏览器下实现效,别的内核浏览器表现不出来。此文五种表格隔行变色的方法兼容所有现代浏览器(很久以前的古董浏览器未测试过)。
方法一:使用th与td标签控制隔行变色
<!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=utf-8" />
<title>方法一 使用th与td标签控制隔行变色 — 实现表格隔行异色的5种解决方案(兼容所有浏览器)</title>
<style type="text/css">
#tab {width: 300px;}
#tab th {font-weight: normal; text-align:left; background: #fff}
#tab td {background: #eee}
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>方法一 使用th与td标签控制隔行变色 — 实现表格隔行异色的5种解决方案(兼容所有浏览器)</title>
<style type="text/css">
#tab {width: 300px;}
#tab th {font-weight: normal; text-align:left; background: #fff}
#tab td {background: #eee}
</style>
<body>
<table id="tab" border="0" cellpadding="0" cellspacing="0">
<tr>
<th height="25">第一个颜色#fff</th>
<th height="25">第一个颜色#fff</th>
</tr>
<tr>
<td height="25">第二个颜色#eee</td>
<td height="25">第二个颜色#eee</td>
</tr>
<tr>
<th height="25">第一个颜色#fff</th>
<th height="25">第一个颜色#fff</th>
</tr>
<tr>
<td height="25">第二个颜色#eee</td>
<td height="25">第二个颜色#eee</td>
</tr>
</table>
</body>
</html>
<table id="tab" border="0" cellpadding="0" cellspacing="0">
<tr>
<th height="25">第一个颜色#fff</th>
<th height="25">第一个颜色#fff</th>
</tr>
<tr>
<td height="25">第二个颜色#eee</td>
<td height="25">第二个颜色#eee</td>
</tr>
<tr>
<th height="25">第一个颜色#fff</th>
<th height="25">第一个颜色#fff</th>
</tr>
<tr>
<td height="25">第二个颜色#eee</td>
<td height="25">第二个颜色#eee</td>
</tr>
</table>
</body>
</html>
方法二:Jjs控制表格隔行变色
<!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=utf-8" />
<title>方法二 js控制表格隔行变色 — 实现表格隔行异色的5种解决方案(兼容所有浏览器)</title>
</head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>方法二 js控制表格隔行变色 — 实现表格隔行异色的5种解决方案(兼容所有浏览器)</title>
</head>
<body>
<table width="300" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>不变色</td>
</tr>
<tbody id="tab">
<tr>
<td>不变色</td>
</tr>
<tr>
<td>变色#eee</td>
</tr>
<tr>
<td>不变色</td>
</tr>
<tr>
<td>变色#eee</td>
</tr>
</tbody>
<tr>
<td>不变色</td>
</tr>
</table>
<table width="300" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>不变色</td>
</tr>
<tbody id="tab">
<tr>
<td>不变色</td>
</tr>
<tr>
<td>变色#eee</td>
</tr>
<tr>
<td>不变色</td>
</tr>
<tr>
<td>变色#eee</td>
</tr>
</tbody>
<tr>
<td>不变色</td>
</tr>
</table>
<script language="JavaScript">
<!–
var TbRow = document.getElementById("tab");
if (TbRow != null)
{
for (var i=0;i<TbRow.rows.length ;i++ )
{
if (TbRow.rows[i].rowIndex%2==1)
{
TbRow.rows[i].style.backgroundColor="";
}
else
{
TbRow.rows[i].style.backgroundColor="#eee";
}
}
}
//–>
</script>
</body>
</html>
<!–
var TbRow = document.getElementById("tab");
if (TbRow != null)
{
for (var i=0;i<TbRow.rows.length ;i++ )
{
if (TbRow.rows[i].rowIndex%2==1)
{
TbRow.rows[i].style.backgroundColor="";
}
else
{
TbRow.rows[i].style.backgroundColor="#eee";
}
}
}
//–>
</script>
</body>
</html>
方法三: js+css实现表格隔行变色&鼠标指向变色A
<!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=utf-8" />
<title>方法三 js+css隔行异色和鼠标指向变色A — 实现表格隔行异色的5种解决方案(兼容所有浏览器)</title>
<style type="text/css">
#tab {border-collapse:collapse;border:solid #999;border-width:1px 0 0 1px;width: 300px;}
#tab td {border:solid #999;border-width:0 1px 1px 0;}
#tab tr.t1 td {background-color:#fff;}/* 第一行的背景色 */
#tab tr.t2 td {background-color:#eee;}/* 第二行的背景色 */
#tab tr.t3 td {background-color:#ccc;}/* 鼠标经过时的背景色 */
</style>
</head>
<body>
<table id="tab">
<tr>
<td>第一个颜色#fff</td>
<td>第一个颜色#fff</td>
</tr>
<tr>
<td>第二个颜色#eee</td>
<td>第二个颜色#eee</td>
</tr>
<tr>
<td>第一个颜色#fff</td>
<td>第一个颜色#fff</td>
</tr>
<tr>
<td>第二个颜色#eee</td>
<td>第二个颜色#eee</td>
</tr>
</table>
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>方法三 js+css隔行异色和鼠标指向变色A — 实现表格隔行异色的5种解决方案(兼容所有浏览器)</title>
<style type="text/css">
#tab {border-collapse:collapse;border:solid #999;border-width:1px 0 0 1px;width: 300px;}
#tab td {border:solid #999;border-width:0 1px 1px 0;}
#tab tr.t1 td {background-color:#fff;}/* 第一行的背景色 */
#tab tr.t2 td {background-color:#eee;}/* 第二行的背景色 */
#tab tr.t3 td {background-color:#ccc;}/* 鼠标经过时的背景色 */
</style>
</head>
<body>
<table id="tab">
<tr>
<td>第一个颜色#fff</td>
<td>第一个颜色#fff</td>
</tr>
<tr>
<td>第二个颜色#eee</td>
<td>第二个颜色#eee</td>
</tr>
<tr>
<td>第一个颜色#fff</td>
<td>第一个颜色#fff</td>
</tr>
<tr>
<td>第二个颜色#eee</td>
<td>第二个颜色#eee</td>
</tr>
</table>
<script type="text/javascript">
<!–
var Ptr=document.getElementById("tab").getElementsByTagName("tr");
function $() {
for (i=1;i<Ptr.length+1;i++) {
Ptr[i-1].className = (i%2>0)?"t1":"t2";
}
}
window.onload=$;
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>
<!–
var Ptr=document.getElementById("tab").getElementsByTagName("tr");
function $() {
for (i=1;i<Ptr.length+1;i++) {
Ptr[i-1].className = (i%2>0)?"t1":"t2";
}
}
window.onload=$;
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>
</body>
</html>
</html>
方法四: js+css实现表格隔行异色&鼠标指向变色B
<!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=utf-8" />
<title>方法四 js+css隔行异色和鼠标指向变色B — 实现表格隔行异色的5种解决方案(兼容所有浏览器)</title>
<style type="text/css" >
.a1{ background-color:#fff;}
.a2{ background-color:#eee;}
#tab{ width:300px; }
</style>
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>方法四 js+css隔行异色和鼠标指向变色B — 实现表格隔行异色的5种解决方案(兼容所有浏览器)</title>
<style type="text/css" >
.a1{ background-color:#fff;}
.a2{ background-color:#eee;}
#tab{ width:300px; }
</style>
<script type="text/javascript">
<!–
function onloadEvent(func){
var one=window.onload
if(typeof window.onload!='function'){
window.onload=func
}
else{
window.onload=function(){
one();
func();
}
}
}
function showtable(){
var tablename=document.getElementById("tab")
var li1=tablename.getElementsByTagName("tr")
for(var i=1 ;i<li1.length;i++){//表格是这里i=1,即第一行不变色。其他为0比较好.
//(i%2==0)?(li1[i].className="a1") : (li1[i].className=”a2″)
//用这个或下面的if(i%2==0){…}
li1[i].onmouseover=function(){
this.style.fontWeight="bold";//当鼠标指向时字体为粗体
this.style.backgroundColor="#ccc";//当鼠标指向时字体颜色
}
li1[i].onmouseout=function(){
this.style.fontWeight=""
this.style.backgroundColor=""
}
if(i%2==0){
li1[i].className="a1";
}else{
li1[i].className="a2";
}
}
}
onloadEvent(showtable);
–>
</script>
</head>
<!–
function onloadEvent(func){
var one=window.onload
if(typeof window.onload!='function'){
window.onload=func
}
else{
window.onload=function(){
one();
func();
}
}
}
function showtable(){
var tablename=document.getElementById("tab")
var li1=tablename.getElementsByTagName("tr")
for(var i=1 ;i<li1.length;i++){//表格是这里i=1,即第一行不变色。其他为0比较好.
//(i%2==0)?(li1[i].className="a1") : (li1[i].className=”a2″)
//用这个或下面的if(i%2==0){…}
li1[i].onmouseover=function(){
this.style.fontWeight="bold";//当鼠标指向时字体为粗体
this.style.backgroundColor="#ccc";//当鼠标指向时字体颜色
}
li1[i].onmouseout=function(){
this.style.fontWeight=""
this.style.backgroundColor=""
}
if(i%2==0){
li1[i].className="a1";
}else{
li1[i].className="a2";
}
}
}
onloadEvent(showtable);
–>
</script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" id="tab">
<tr>
<td colspan="2" bgcolor="#FFFFCC">表格标题行,不设变色效果</td>
</tr>
<tr>
<td>第一个颜色#fff</td>
<td>第一个颜色#fff</td>
</tr>
<tr>
<td>第二个颜色#eee</td>
<td>第二个颜色#eee</td>
</tr>
<tr>
<td>第一个颜色#fff</td>
<td>第一个颜色#fff</td>
</tr>
<tr>
<td>第二个颜色#eee</td>
<td>第二个颜色#eee</td>
</tr>
</table>
</body>
</html>
<table border="0" cellpadding="0" cellspacing="0" id="tab">
<tr>
<td colspan="2" bgcolor="#FFFFCC">表格标题行,不设变色效果</td>
</tr>
<tr>
<td>第一个颜色#fff</td>
<td>第一个颜色#fff</td>
</tr>
<tr>
<td>第二个颜色#eee</td>
<td>第二个颜色#eee</td>
</tr>
<tr>
<td>第一个颜色#fff</td>
<td>第一个颜色#fff</td>
</tr>
<tr>
<td>第二个颜色#eee</td>
<td>第二个颜色#eee</td>
</tr>
</table>
</body>
</html>
方法五: js+css实现表格隔行异色&鼠标指向变色C
<!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=utf-8" />
<title>05 js+css隔行变色和鼠标指向变色C — 实现表格隔行异色的4种解决方案(兼容所有浏览器)</title>
<style type="text/css" >
.a1{ background-color:#fff;}
.a2{ background-color:#eee;}
#tab{ width:300px; }
dt{background-color:#0066CC; color:#FFFFFF;}
dd{margin:0px; display:block;}
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>05 js+css隔行变色和鼠标指向变色C — 实现表格隔行异色的4种解决方案(兼容所有浏览器)</title>
<style type="text/css" >
.a1{ background-color:#fff;}
.a2{ background-color:#eee;}
#tab{ width:300px; }
dt{background-color:#0066CC; color:#FFFFFF;}
dd{margin:0px; display:block;}
</style>
<script type="text/javascript">
<!–
function onloadEvent(func){
var one=window.onload
if(typeof window.onload!='function'){
window.onload=func
}
else{
window.onload=function(){
one();
func();
}
}
}
function showtable(){
var tablename=document.getElementById("tab")
var li1=tablename.getElementsByTagName("dd")
for(var i=0 ;i<li1.length;i++){//表格是这里i=1,其他为0比较好.
//(i%2==0)?(li1[i].className="a1") : (li1[i].className=”a2″)//用这个或下面的if(i%2==0){…}
li1[i].onmouseover=function(){
this.style.fontWeight="";
this.style.backgroundColor="#ccc";//当鼠标指向时字体颜
}
li1[i].onmouseout=function(){
this.style.fontWeight=""
this.style.backgroundColor=""
}
if(i%2==0){
li1[i].className="a1";
}else{
li1[i].className="a2";
}
}
}
onloadEvent(showtable);
–>
</script>
</head>
<!–
function onloadEvent(func){
var one=window.onload
if(typeof window.onload!='function'){
window.onload=func
}
else{
window.onload=function(){
one();
func();
}
}
}
function showtable(){
var tablename=document.getElementById("tab")
var li1=tablename.getElementsByTagName("dd")
for(var i=0 ;i<li1.length;i++){//表格是这里i=1,其他为0比较好.
//(i%2==0)?(li1[i].className="a1") : (li1[i].className=”a2″)//用这个或下面的if(i%2==0){…}
li1[i].onmouseover=function(){
this.style.fontWeight="";
this.style.backgroundColor="#ccc";//当鼠标指向时字体颜
}
li1[i].onmouseout=function(){
this.style.fontWeight=""
this.style.backgroundColor=""
}
if(i%2==0){
li1[i].className="a1";
}else{
li1[i].className="a2";
}
}
}
onloadEvent(showtable);
–>
</script>
</head>
<body>
<dl id="tab">
<dt>标题行,不设变色效果</dt>
<dd>第一个颜色#fff</dd>
<dd>第二个颜色#eee</dd>
<dd>第一个颜色#fff</dd>
<dd>第二个颜色#eee</dd>
</dl>
<dl id="tab">
<dt>标题行,不设变色效果</dt>
<dd>第一个颜色#fff</dd>
<dd>第二个颜色#eee</dd>
<dd>第一个颜色#fff</dd>
<dd>第二个颜色#eee</dd>
</dl>
</body>
</html>
</html>
简单的隔行变色:
<style type="text/css">
<!--
tr {background-color:expression((this.sectionRowIndex%2==0)?"#E1F1F1":"#F0F0F0")}
-->
</style>
<!--
tr {background-color:expression((this.sectionRowIndex%2==0)?"#E1F1F1":"#F0F0F0")}
-->
</style>
<table>
<tr><td>第1行</td><td>第1列</td></tr>
<tr><td>第2行</td><td>第2列</td></tr>
<tr><td>第3行</td><td>第3列</td></tr>
<tr><td>第4行</td><td>第4列</td></tr>
<tr><td>第5行</td><td>第5列</td></tr>
</table>
<tr><td>第1行</td><td>第1列</td></tr>
<tr><td>第2行</td><td>第2列</td></tr>
<tr><td>第3行</td><td>第3列</td></tr>
<tr><td>第4行</td><td>第4列</td></tr>
<tr><td>第5行</td><td>第5列</td></tr>
</table>
-------------------------------
每个单元格变色:
<style type="text/css">
<!--
tr {background-color:expression((this.sectionRowIndex%2==0)?"red":"blue")}
td {background-color:expression((this.cellIndex%2==0)?"":((this.parentElement.sectionRowIndex%2==0)?"green":"yellow"))}
-->
</style>
<!--
tr {background-color:expression((this.sectionRowIndex%2==0)?"red":"blue")}
td {background-color:expression((this.cellIndex%2==0)?"":((this.parentElement.sectionRowIndex%2==0)?"green":"yellow"))}
-->
</style>
<table>
<tr><td>第1行</td><td>第1列</td></tr>
<tr><td>第2行</td><td>第2列</td></tr>
<tr><td>第3行</td><td>第3列</td></tr>
<tr><td>第4行</td><td>第4列</td></tr>
<tr><td>第5行</td><td>第5列</td></tr>
</table>
<tr><td>第1行</td><td>第1列</td></tr>
<tr><td>第2行</td><td>第2列</td></tr>
<tr><td>第3行</td><td>第3列</td></tr>
<tr><td>第4行</td><td>第4列</td></tr>
<tr><td>第5行</td><td>第5列</td></tr>
</table>
------------------------
以上都用到expression,实现变得很方便,但是,经测试,在IE6(其它版本我不知道)上很正常,在firefox上无任何反应…… ,要想在firefox上也有此效果,就要用第二种方法