我需要编写一个函数(或构建在下面的代码上),它将突出显示表格中的特定行。它需要始终忽略表头,因此在表头之后开始行数为 0。
<style type="text/css">
#myTbl {
border: 1px solid black
}
#myTbl td, th {
border: 1px solid black
}
#myTbl tr.normal td {
color: black;
background-color: white;
}
#myTbl tr.highlighted td {
color: white;
background-color: gray;
}
</style>
<table id="myTbl">
<thead>
<tr>
<th>ID</th>
<th>CreatedDate</th>
<th>Name</th>
<th>Colour</th>
</tr>
</thead>
<tbody>
<tr>
<td>DFRF</td>
<td>05/03/2010</td>
<td>Lamp</td>
<td>Blue</td>
</tr>
Ect...
</tbody>
</table>
<script type="text/javascript">
var table = document.getElementById("myTbl");
var thead = table.getElementsByTagName("thead")[0];
var tbody = table.getElementsByTagName("tbody")[0];
tbody.onclick = function (e) {
e = e || window.event;
var td = e.target || e.srcElement;
//so must be no other elements inside the td
var row = td.parentNode;
alert('Row is ' + (row.rowIndex - 1))
if (this.lst&&this.lst!=row){
this.lst.className='';
}
row.className = row.className==="highlighted" ? "" : "highlighted";
this.lst=row;
}
thead.onclick = function (e) {
e = e || window.event;
var th = e.target || e.srcElement;
//so must be no other elements in the th
alert(th.innerHTML);
}
</script>
就像是
function goToRow('2')
选择第 2 行。
需要帮助请