中文(繁体)
<!--第一步:把下列程式碼加入head區域內--> <head> <STYLE> th{background-color:#999999;} .trOdd{background-color:#FFFFFF;} .trEven{background -color:#CCCCCC;} </STYLE> <SCRIPT language="javascript"> /** * 自動掃描表格,描繪奇偶行的顏色* 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樣式*/ //more javascript from http://www.downcodes.com 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 = ""; } } </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'); ColsetColor(document .all.tableRow,'trOdd','trEven'); changeTag = false; } else { setRowColor(document.all.tableRow,'trOdd','trEven'); setColColor(document.all.table,'trOdd', 'trEven'); changeTag = true; } } </SCRIPT> </head> <!--第二步:修改BODY內容為下面內容--> <body onLoad="init()"> <!--第三步:把下列程式碼加到BODY區域內--> <TABLE width="70%" border="1" cellpadding="0" cellspacing="1" bordercolor="#000000" 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="1" cellpadding="0" cellspacing="1" bordercolor="#000000" 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> <!--代碼說明:說明: 1、在<body>中加入onLoad="init()" ,變為<body onLoad="init()"> 2、函數function init() { setRowColor(document.all.tableRow,'trOdd','trEven'); setColColor(document.all.tableCol,'trOdd','trEven '); } tableRow、tableCol 為設定顏色的表格ID值setRowColor 為設定隔行顏色函數setColColor 為設定隔列顏色函數-->