简体   繁体   English

使用纯JavaScript在点击时切换行颜色为红色和表颜色为白色(无jquery)

[英]Toggle row color red and table color white on click with pure javascript (no jquery)

I'm trying to duplicate the following JSFiddle in pure javascript without relying on jquery or other methods. 我正在尝试在纯JavaScript中复制以下JSFiddle ,而不依赖于jquery或其他方法。

 $('table tr').each(function(a,b){ $(b).click(function(){ $('table tr').css('background','#ffffff'); $(this).css('background','#ff0000'); }); }); 
 <table> <tr> <td>data</td> <td>data</td> </tr> <tr> <td>data</td> <td>data</td> </tr> </table> 

The first click highlights a row, the second click highlights the next selected row and removes the highlight from the previous row. 第一次单击突出显示一行,第二次单击突出显示下一个选定的行,并从上一行中删除突出显示。 Also, I've tried implementing this code in my work but it wouldn't work at all and provides 0 error messages to give me a clue as to what is going on. 另外,我已经尝试在工作中实现此代码,但是它根本不起作用,并提供了0条错误消息,为我提供了发生情况的线索。 Copy/pasting this fiddle into a new one does not reproduce the results and this seems to be a common theme while trying to track down an answer to this problem. 将这个小提琴复制/粘贴到一个新的提琴中并不能重现结果,这在试图寻找答案的过程中似乎是一个普遍的主题。 I've searched all over stackoverflow and haven't been able to find a working solution that relies only on css, javascript, and/or html. 我已经在stackoverflow上进行了全面搜索,但找不到一个仅依赖于CSS,JavaScript和/或html的有效解决方案。

Use [].forEach.call to iterate tr elements. 使用[].forEach.call迭代tr元素。 Use of Function#call is needed as document.querySelectorAll does not return Array . 需要使用Function#call ,因为document.querySelectorAll不返回Array

 [].forEach.call(document.querySelectorAll('table tr'), function(el) { el.addEventListener('click', function() { [].forEach.call(document.querySelectorAll('table tr'), function(el) { el.style.background = '#fff'; }); this.style.background = '#f00' }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <table border="1" cellspacing="1" width="100%" id="table1"> <tr> <th>Column1</th> <th>Column2</th> <th>Column3</th> <th>Column4</th> <th>Column5</th> </tr> <tr> <td>data</td> <td>data</td> <td>data</td> <td>data</td> <td>data</td> </tr> <tr> <td>data</td> <td>data</td> <td>data</td> <td>data</td> <td>data</td> </tr> <tr> <td>data</td> <td>data</td> <td>data</td> <td>data</td> <td>data</td> </tr> <tr> <td>data</td> <td>data</td> <td>data</td> <td>data</td> <td>data</td> </tr> </table> 

https://jsfiddle.net/M4V3R1C8/7L2typ5t/1/ This script allows you to do exactly what I was looking for. https://jsfiddle.net/M4V3R1C8/7L2typ5t/1/此脚本可让您执行我一直在寻找的内容。 The only downside is that you cannot activate the top row. 唯一的缺点是您无法激活第一行。 Also, I can't seem to change the .selected name as it will stop working. 另外,我似乎无法更改.selected名称,因为它将停止工作。 Good enough for now, onto the next problem. 现在已经足够好,进入下一个问题。

function selected(){
    var index,
      table = document.getElementById("dps");
  for(var i = 1; i < table.rows.length; i++) {
        table.rows[i].onclick = function() {
        if(typeof index !== "undefined"){
          table.rows[index].classList.toggle("selected");
      }
      console.log(typeof index);
      index = this.rowIndex;
      this.classList.toggle("selected");
      console.log(typeof index);
    };
  }
}
selected();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM