使用函數完成表格奇偶行的顏色設定

來源:互聯網
上載者:User
函數 對於表格,為了讓使用者瀏覽時不會看錯行,一般使用奇偶行機制,可以採用後台經過判斷奇偶來給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>




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。