Javascript 在逐行单击时高亮显示和取消高亮显示表格行

Javascript 在逐行单击时高亮显示和取消高亮显示表格行,javascript,html,Javascript,Html,我处理这个问题已经有一段时间了,运气不好 请注意。没有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.sty

我处理这个问题已经有一段时间了,运气不好

请注意。没有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;
   }
    }
 }
}
<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>
函数高亮显示(){
var table=document.getElementById('dataTable');
for(var i=0;i
HTML如下所示

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;
   }
    }
 }
}
<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>

数据1数据2
数据1数据2
数据1数据2
当前,当我单击它时会更改颜色,但是当我单击第二行时,第一行仍然保持高亮显示。你能帮我完成这个任务吗


谢谢。

您需要取消高亮显示其他行,因为现在您正在更改单击的行

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;
 }
}
函数高亮显示(){
var table=document.getElementById('dataTable');
for(var i=0;i
我已经在

Javascript:

function toggleClass(el, className) {
    if (el.className.indexOf(className) >= 0) {
        el.className = el.className.replace(className,"");
    }
    else {
        el.className  += className;
    }
}
HTML:


我最近也遇到了同样的问题,只是用纯JS解决了它 这是我的版本

函数高亮显示_行(){
var table=document.getElementById('display-table');
var cells=table.getElementsByTagName('td');
对于(变量i=0;i
取消选择时无法取消高亮显示行

 <script type="text/javascript">

  function highlight(){
  var hilite;  
 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='#fdee9a';
    this.hilite = true;
   }
   else{
    this.style.backgroundColor=this.origColor;
    this.hilite = false;
   }
    }
 }
}

function unhighlight(){
  var hilite;
 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;
 }
}

  </script>

函数高亮显示(){
白榴石;
var table=document.getElementById('dataTable');
for(var i=0;i
@OneTrickPony我在这里打字的时候打错了。更新。您的代码有一个小问题,我似乎无法找出问题所在。现在需要单击两次才能在页面加载时突出显示表行。在最初的两次单击后,表正常工作。有什么想法吗?对不起,我出去了一会儿。加载页面后,尝试为每一行初始化
hilite
。为什么要投否决票?这可以根据需要使用较少的代码和良好的解决方案。当选择新的时,如何取消选择其余的?
 <script type="text/javascript">

  function highlight(){
  var hilite;  
 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='#fdee9a';
    this.hilite = true;
   }
   else{
    this.style.backgroundColor=this.origColor;
    this.hilite = false;
   }
    }
 }
}

function unhighlight(){
  var hilite;
 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;
 }
}

  </script>