I've been at this problem for awhile with no luck.
Please note. No jquery =/
The JS code I have is as following
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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
The HTML is as following
<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>
Run Code Online (Sandbox Code Playgroud)
Currently when I click it changes color, however when I click on the second row the first row still remains highlighted. Could you please assist me in accomplishing this task with no jquery?
Thank you.
我在JSFiddle上创建了以下工作示例
JavaScript:
function toggleClass(el, className) {
if (el.className.indexOf(className) >= 0) {
el.className = el.className.replace(className,"");
}
else {
el.className += className;
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<table class="gridview">
<tr onclick="toggleClass(this,'selected');"><td></td><td></td></tr>
<tr onclick="toggleClass(this,'selected');"><td></td><td></td></tr>
<tr onclick="toggleClass(this,'selected');"><td></td><td></td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)
CSS:
.gridview .selected, .gridview tbody .selected {
background-color: #6ccbfb;
color: #fff;
}
Run Code Online (Sandbox Code Playgroud)
我最近遇到了同样的问题,只是使用纯 JS 解决了它 这是我的版本 https://jsfiddle.net/armaandhir/Lgt1j68s/
function highlight_row() {
var table = document.getElementById('display-table');
var cells = table.getElementsByTagName('td');
for (var i = 0; i < cells.length; i++) {
// Take each cell
var cell = cells[i];
// do something on onclick event for cell
cell.onclick = function () {
// Get the row id where the cell exists
var rowId = this.parentNode.rowIndex;
var rowsNotSelected = table.getElementsByTagName('tr');
for (var row = 0; row < rowsNotSelected.length; row++) {
rowsNotSelected[row].style.backgroundColor = "";
rowsNotSelected[row].classList.remove('selected');
}
var rowSelected = table.getElementsByTagName('tr')[rowId];
rowSelected.style.backgroundColor = "yellow";
rowSelected.className += " selected";
msg = 'The ID of the company is: ';
msg += rowSelected.cells[0].innerHTML;
msg += '\nThe cell value is: ' + this.innerHTML;
alert(msg);
}
}
} //end of function
window.onload = highlight_row;
Run Code Online (Sandbox Code Playgroud)
您需要取消突出显示其他行,因为现在您只是更改单击的行。
function highlight(){
var table = document.getElementById('dataTable');
for (var i=0;i < table.rows.length;i++){
table.rows[i].onclick= function () {
if(!this.hilite){
unhighlight();
this.origColor=this.style.backgroundColor;
this.style.backgroundColor='#BCD4EC';
this.hilite = true;
}
else{
this.style.backgroundColor=this.origColor;
this.hilite = false;
}
}
}
}
function unhighlight(){
var table = document.getElementById('dataTable');
for (var i=0;i < table.rows.length;i++){
var row = table.rows[i];
row.style.backgroundColor=this.origColor;
row.hilite = false;
}
}
Run Code Online (Sandbox Code Playgroud)