函数
对于表格,为了让用户浏览时不会看错行,一般使用奇偶行机制,可以采用后台经过判断奇偶来给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.all.tableRow,'trOdd','trEven');
setColColor(document.all.tableCol,'trOdd','trEven');
}
function change()
{
if(changeTag)
{
setRowColor(document.all.tableCol,'trOdd','trEven');
setColColor(document.all.tableRow,'trOdd','trEven');
changeTag = false;
}
else
{
setRowColor(document.all.tableRow,'trOdd','trEven');
setColColor(document.all.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>