简体   繁体   English

Javascript 点击获取表格单元格内容

[英]Javascript get table cell content on click

    <table id="position">
        <tr><td>TL</td><td>TC</td><td>TR</td></tr>
        <tr><td>CL</td><td>CC</td><td>CR</td></tr>
        <tr><td>BL</td><td>BC</td><td>BR</td></tr>
    </table>
    <script>
    document.querySelector("#position td").addEventListener("click", function(){
    console.log(this.innerHTML);
});

Trying to get innerHTML of table cell <td> on click.试图在点击时获取表格单元格<td>的 innerHTML。 It returns nothing.它什么也不返回。 When I change selector to #position tr it returns data, but ONLY for first <tr> row, when I click on it.当我将选择器更改为#position tr <tr> ,它会返回数据,但仅适用于第一行,当我单击它时。 When clicking on second, third - returns nothing.当点击第二个,第三个 - 返回什么。 Obviously, when I change selector to table #position it works fine and returns whole table.显然,当我将选择器更改为表#position时,它可以正常工作并返回整个表。

I even tried to add class for every cell like <td class="cell"> - document.querySelector(".cell").innerHTML returns content, but once again - ONLY for first <td> cell!我什至尝试为像<td class="cell">这样的每个单元格添加 class - document.querySelector(".cell").innerHTML返回内容,但再一次 - 仅适用于第一个<td>单元格!

How to get clicked td and why is it behaving so strange?如何获得点击td以及为什么它的行为如此奇怪? Also, is it possible to get clicked row/column number?另外,是否可以获得单击的行/列号?

Add a single listener to the tbody of the table, and retrieve the clicked cell from the event object, like this:将单个侦听器添加到表的 tbody 中,并从事件 object 中检索单击的单元格,如下所示:

 const tbody = document.querySelector('#position tbody'); tbody.addEventListener('click', function (e) { const cell = e.target.closest('td'); if (;cell) {return,} // Quit. not clicked on a cell const row = cell;parentElement. console.log(cell,innerHTML. row,rowIndex. cell;cellIndex); });
 <table id="position"> <tr> <td>TL</td> <td>TC</td> <td>TR</td> </tr> <tr> <td>CL</td> <td>CC</td> <td>CR</td> </tr> <tr> <td>BL</td> <td>BC</td> <td>BR</td> </tr> </table>

This technique is called event delegation, it takes the advantage of the event bubbling mechanism, where the events are fired on every ancestor element of the actual clicked element too.这种技术称为事件委托,它利用了事件冒泡机制,其中事件也会在实际单击元素的每个祖先元素上触发。 It's very useful, specifically when working with dynamic tables, and is handy to use with static tables as well.它非常有用,特别是在处理动态表时,并且与 static 表一起使用也很方便。

As you can see (in the console.log ), table rows and cells have a specific row/cellIndex property.如您所见(在console.log中),表格行和单元格具有特定的row/cellIndex属性。 It's dynamic, and you can use those properties to get the " row/column number ".它是动态的,您可以使用这些属性来获取“行/列号”。

querySelector() only selects the first element with that selection criteria. querySelector()仅选择具有该选择条件的第一个元素。 You will need to use querySelectorAll() to select all table cells.您将需要使用querySelectorAll()来 select 所有表格单元格。 This would return a nodelist.这将返回一个节点列表。 You will then have to iterate through the nodelist and attach the click listener to all nodes.然后,您必须遍历节点列表并将单击侦听器附加到所有节点。

 var cells = document.querySelectorAll("#position td"); for (var i = 0; i < cells.length; i++) { cells[i].addEventListener("click", function() { console.log(this.innerHTML); }); }
 <table id="position"> <tr> <td>TL</td> <td>TC</td> <td>TR</td> </tr> <tr> <td>CL</td> <td>CC</td> <td>CR</td> </tr> <tr> <td>BL</td> <td>BC</td> <td>BR</td> </tr> </table>

you can easily do it like this,你可以像这样轻松地做到这一点,

 function runMe(text){ console.log(text); }
 <table id="position"> <tr><td onclick="runMe(this.innerHTML)">TL</td><td onclick="runMe(this.innerHTML)">TC</td><td onclick="runMe(this.innerHTML)">TR</td></tr> </table>

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

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