[英]JavaScript - Find row's first cell value when clicked
I have the following table where each row has 4 cells with a button on the last cell: 我有下表,其中每行有4个单元格,最后一个单元格上有一个按钮:
<table class='Table1'>
<tr>
<td>
ABC
</td>
<td>
DEF
</td>
<td>
GHI
</td>
<td>
<button class='return'>return</button>
</td>
</tr>
<tr>
<td>
JKL
</td>
<td>
MNO
</td>
<td>
PQR
</td>
<td>
<button class='return'>return</button>
</td>
</tr>
</table>
When the button is clicked I want to find the first cell value of the same row using Javascript
(no jquery
). 当单击按钮时,我想使用
Javascript
(没有jquery
)找到同一行的第一个单元格值。 For example if I click on the return
button on the first row, it should return ABC
例如,如果我单击第一行上的
return
按钮,它将返回ABC
You can use the function closest
to go up and get the element tr
and then use the function querySelector
to get the first element td
. 您可以使用
closest
的函数来获取元素tr
,然后使用querySelector
函数来获取第一个元素td
。
Array.prototype.forEach.call(document.querySelectorAll('button'), function(b) { b.addEventListener('click', function() { console.log(this.closest('tr').querySelector('td').textContent.trim()); }); })
<table class='Table1'> <tr> <td> ABC </td> <td> DEF </td> <td> GHI </td> <td> <button class='return'>return</button> </td> </tr> <tr> <td> JKL </td> <td> MNO </td> <td> PQR </td> <td> <button class='return'>return</button> </td> </tr></table>
HTMLTableElement.rows and HTMLTableRowElement.cells for easy table component manipulation. HTMLTableElement.rows和HTMLTableRowElement.cells可简化表组件的操作。
Event Delegation to efficiently control the click event of every tag in the table with only one event listener. 事件委托仅使用一个事件侦听器即可有效控制表中每个标记的click事件。
event.target
to reference the button that was clicked, event.currentTarget
to reference the table. event.target
引用被单击的按钮, event.currentTarget
引用该表。
.contains()
to get the row that the clicked button resided in. .contains()
以获取单击的按钮所在的行。
matches()
to determine if event.target had a class called .return
. .return
matches()
确定event.target是否具有名为.return
的类。
var T = document.querySelector('.Table1'); T.addEventListener('click', function(e) { var out = document.getElementById('out'); // Clicked tag (ie a button.return) var tgt = e.target; // Tag registered to event (ie table.Table1) var cur = e.currentTarget; // A HTMLCollection of every <tr> in .table1 var Rows = cur.rows; // Declare blank string; var txt = ""; /* if clicked tag is NOT .Table1 AND it has class .return... */ if (tgt !== cur && tgt.matches('.return')) { /* Begin count at 0; keep counting until the last row; increment cont by 1 */ for (let i = 0; i < Rows.length; i++) { // if this row has the clicked button as a descendant... if (Rows[i].contains(tgt)) { // Get the text of that row's first cell txt = Rows[i].cells[0].textContent; } } // Set the value of output#out to extracted text out.value = txt; } });
table, td { border: 3px solid #000; } #out { display: block }
<output id='out'> </output> <table class='Table1'> <tr> <td> ABC </td> <td> DEF </td> <td> GHI </td> <td> <button class='return'>return</button> </td> </tr> <tr> <td> JKL </td> <td> MNO </td> <td> PQR </td> <td> <button class='return'>return</button> </td> </tr> <tr> <td> HHH </td> <td> DEF </td> <td> GHI </td> <td> <button class='return'>return</button> </td> </tr> <tr> <td> 123 </td> <td> DEF </td> <td> GHI </td> <td> <button class='return'>return</button> </td> </tr> <tr> <td> XYZ </td> <td> DEF </td> <td> GHI </td> <td> <button class='return'>return</button> </td> </tr> <tr> <td> XXX </td> <td> DEF </td> <td> GHI </td> <td> <button class='return'>return</button> </td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.