我一直在这个问题上一段时间没有运气。
请注意。没有 jquery =/
我拥有的JS代码如下
function highlight(){
var table = document.getElementById('dataTable');
for (var i=0;i < table.rows.length;i++){
table.rows[i].onclick= function () {
if(!this.hilite){
this.origColor=this.style.backgroundColor;
this.style.backgroundColor='#BCD4EC';
this.hilite = true;
}
else{
this.style.backgroundColor=this.origColor;
this.hilite = false;
}
}
}
}
HTML如下
<table id="dataTable">
<tr onclick="highlight()"><td>Data1</td><td>Data2</td></tr>
<tr onclick="highlight()"><td>Data1</td><td>Data2</td></tr>
<tr onclick="highlight()"><td>Data1</td><td>Data2</td></tr>
</table>
目前,当我单击它时,它会改变颜色,但是当我单击第二行时,第一行仍然突出显示。您能在没有 jquery 的情况下帮助我完成这项任务吗?
谢谢你。