對於表格,為了讓使用者瀏覽時不會看錯行,一般使用奇偶行機制,可以採用後台經過判斷奇偶來給table著色.
這裡提供一種簡便的方法,即是頁面載入完畢後用js指令碼判斷,對錶格進行著色.如下:
<!-- 歡迎轉載,請保留作者及其出處,謝謝 -->
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>table Color</TITLE>
<STYLE>
table{border-top:1px solid black;border-left:1px solid black;cursor:default;}
td{border-bottom:1px solid black;border-right:1px solid black;height:22px;}
th{border-bottom:2px solid black;border-right:1px solid black;background-color:#999999;}
.trOdd{background-color:#FFFFFF;}
.trEven{background-color:#CCCCCC;}
</STYLE>
<SCRIPT language="javascript">
/*********************** setTableColor.js ***********************************/
/**
* added by LxcJie 2004.6.25
* 自動掃描表格,描繪奇偶行的顏色
* oTable:目標表格 oddClass:奇數行的css樣式 evenClass:偶數行的css樣式
*/
function setRowColor(oTable,oddClass,evenClass)
{
resetTableColor(oTable);
for(var i=1; i<oTable.rows.length; i++)
{
if(i % 2 == 0)
oTable.rows[i].className = evenClass;
else
oTable.rows[i].className = oddClass;
}
}
/**
* added by LxcJie 2004.6.28
* 自動掃描表格,描繪奇偶列的顏色
* oTable:目標表格 oddClass:奇數列的css樣式 evenClass:偶數列的css樣式
*/
function setColColor(oTable,oddClass,evenClass)
{
resetTableColor(oTable);
for(var i=1; i<oTable.rows.length; i++)
{
for(var j=0; j<oTable.rows[i].cells.length; j++)
{
if(j % 2 == 0)
oTable.rows[i].cells[j].className = evenClass;
else
oTable.rows[i].cells[j].className = oddClass;
}
}
}
//清空所有tr和td的樣式
function resetTableColor(oTable)
{
for(var i=1; i<oTable.rows.length; i++)
{
oTable.rows[i].className = "";
for(var j=0; j<oTable.rows[i].cells.length; j++)
oTable.rows[i].cells[j].className = "";
}
}
/*********************** setTableColor.js 結束 ***********************************/
</SCRIPT>
<SCRIPT language="javascript">
window.changeTag = true;
function init()
{
setRowColor(document.getElementById("tableRow"),'trOdd','trEven');
setColColor(document.getElementById("tableCol"),'trOdd','trEven');
}
function change()
{
if(changeTag)
{
setRowColor(document.getElementById("tableCol"),'trOdd','trEven');
setColColor(document.getElementById("tableRow"),'trOdd','trEven');
changeTag = false;
}
else
{
setRowColor(document.getElementById("tableRow"),'trOdd','trEven');
setColColor(document.getElementById("tableCol"),'trOdd','trEven');
changeTag = true;
}
}
</SCRIPT>
</HEAD>
<BODY onLoad="init()">
<TABLE width="70%" border="0" cellspacing="0" cellpadding="0" id="tableRow">
<TR>
<TH scope="col">col1</TH>
<TH scope="col">col2</TH>
<TH scope="col">col3</TH>
</TR>
<TR>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</TR>
<TR>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</TR>
<TR>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</TR>
<TR>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</TR>
<TR>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</TR>
<TR>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</TR>
</TABLE>
<P>
<TABLE width="70%" border="0" cellspacing="0" cellpadding="0" id="tableCol">
<TR>
<TH width="25%" scope="col">col1</TH>
<TH width="25%" scope="col">col2</TH>
<TH width="25%" scope="col">col3</TH>
<TH width="25%" scope="col">col4</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>
<TR>
<TD> </TD>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</TR>
</TABLE><p>
<input type="button" value=" 交 換 " onClick="change()" style="border:1px solid black; ">
</BODY>
</HTML>