<table id="tb1">
<tr οnclick="javascript:alert(this.sectionRowIndex)"><td>第一行</td></tr>
<tr οnclick="javascript:alert(this.sectionRowIndex)"><td>第二行</td></tr>
</table>
通过tr标签.sectionRowIndex可以获取tr在table里的索引位置。索引从0开始,类似数组下标。
结合jquery可以快速简单制作隔行换色的功能。
function setTrColor(TableID) {
$("#" + TableID).find("Tr").each(function () {
var RowIndex = parseInt(this.sectionRowIndex);
if (RowIndex > 0 && RowIndex < $("#" + TableID).find("Tr").length - 1) {
this.style.backgroundColor = (RowIndex % 2 == 0) ? "#fff" : "#444";
this.onmouseover = function () {
this.style.backgroundColor = "#cfc";
}
this.onmouseout = function () {
this.style.backgroundColor = (RowIndex % 2 == 0) ? "#fff" : "#444";
}
}
});
}