简体   繁体   English

JavaScript-单击时查找行的第一个单元格值

[英]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> 

APIs and Patterns API和模式


Demo 演示

 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'>&nbsp;</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.

相关问题 JavaScript 单击同一行上的按钮时获取第二个单元格值 - JavaScript Getting second <td> cell value when clicked on a button on the same row 单击特定列时获取表行单元格值 - Getting a table row cell value when a specific column is is clicked 单击表行时如何从单元格获取值 - How to get the value from a cell when table row is clicked javascript根据第一个单元格中的值制作可点击的行 - javascript make clickable row based on value in first cell 从连续第一个单元格获取值以使用Javascript提交 - Getting the value from first cell in a row to submit using Javascript 获取单击列中第一个单元格的内容以及HTML表格中单击行的第一个单元格 - Get contents of the first cell in the clicked column and the first cell of the clicked row in a HTML table 单击网格中的单元格时,如何获取单元格的列标题和行的第一个td? - How to get a cell's column header and row's first td when I click a cell in grid? 单击时如何动态更改表单元格中的链接值时表格行的背景色 - how to dynamically change a table row background color when a link value within its cell changes when clicked 查找具有特定单元格值的行 - find row with a specific cell value Javascript HTML Table根据另一个单元格的onclick获取第一列的单元格值 - Javascript HTML Table get first column's cell value based on another cell's onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM