IE8 以上的版本,只需用 CSS 即可:
tr { background-color: expression('#F8F8F8,#EFEFEF'.split(',')[rowIndex%2]); }
IE8 以下的版本,則需要 CSS 與 JS 共同完成。首先定義兩個 CSS 類:
.tr1 { background-color: #fff; }
.tr2 { background-color: #00ff; }
然後寫 JS:
onload = function () {
var trs = document.getElementById("tbid").getElementsByTagName("tr"); // 取得 tbid 這個 table 中所有 tr
for (var i = 0; i < trs.length; i++) {
if (i % 2 == 0)
trs[i].className = "tr1";
else
trs[i].className = "tr2";
}
}
透過 this.rowIndex 的 CSS expression 或 JS 遍歷加上奇偶 class,都能實現表格的隔行換色。作者:chenhuzi。