简体   繁体   English

如何定位我单击的表格行中每个单元格的内部 html?

[英]How can I target the inner html of each cell in a table row that I've clicked on?

I am trying to create a function on my website so that users can click a "save" button on a particular row of this table that will pull up the innerHtml of the entire row element that they have selected and then Im going to put that info into another page for them as their saved hiking trails.我正在尝试在我的网站上创建一个函数,以便用户可以单击该表特定行上的“保存”按钮,该按钮将拉出他们选择的整个行元素的 innerHtml,然后我将把该信息进入另一个页面,作为他们保存的远足路径。

I have been trying to do this by adding click event listeners to the tables and then accessing the information of the table row through the target.我一直在尝试通过向表格添加点击事件侦听器然后通过目标访问表格行的信息来做到这一点。

Does anyone know how I can access the inner html of the whole row and not just the cell that the save button is in?有谁知道我如何访问整行的内部 html 而不仅仅是保存按钮所在的单元格?

Here is my html:这是我的html:

<table class="table">
            <tr>
              <th>Trails</th>
              <th>Province</th>
              <th>Country</th>
            </tr>
            <tr>
              <td class="tableRow">Lion's Head</td>
              <td class="tableRow">Western Cape</td>
              <td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
            </tr>
            <tr>
              <td class="tableRow">Pipe Track</td>
              <td class="tableRow">Western Cape</td>
              <td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
            </tr>
            <tr>
              <td class="tableRow">Skeleton Gorge</td>
              <td class="tableRow">Western Cape</td>
              <td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
            </tr>
            <tr>
              <td class="tableRow">Table Mountain</td>
              <td class="tableRow">Western Cape</td>
              <td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
            </tr>
            <tr>
              <td class="tableRow">King Fisher Trail</td>
              <td class="tableRow">Western Cape</td>
              <td class="tableRow">South Africa<button id="save" button class="save">Save</button></td>
            </tr>
            <tr>
              <td class="tableRow">Robberg Peninsula</td>
              <td class="tableRow">Western Cape</td>
              <td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
            </tr>
            <tr>
              <td class="tableRow">Diep Walle Forest Walk</td>
              <td class="tableRow">Western Cape</td>
              <td class="tableRow">South Africa<button id="save" class="save">Save</button></td>
            </tr>
          </table>

And here is the javascript that I have tried to use to target the row that I need.这是我试图用来定位我需要的行的javascript。

const save = document.querySelector(".save");
console.log(save);

save.addEventListener("click", (e) => {
  let row = document.getElementsByClassName("tableRow");
  console.log(row[1], row[2], row[3]);
});

This javascript is not targeting the right table cells.此 javascript 未针对正确的表格单元格。

You cannot have more than one element in your page with id="save" - id must be unique at all times, per-document .您的页面中不能有多个id="save"的元素 -每个文档的 id 在任何时候都必须是唯一的 For multiple elements, use a class instead.对于多个元素,请改用一个类。

That being said, use event.target.closest to find the row from a delegate listener (that is, you install the click listener on an ancestor element and capitalize on the bubbling mechanism of events).话虽如此,使用event.target.closest从委托侦听器中查找行(也就是说,您将单击侦听器安装在祖先元素上并利用事件的冒泡机制)。 This way you also only need one listener.这样你也只需要一个听众。

 const table = document.getElementById('myTable'); table.addEventListener("click", (event) => { if (event.target.matches('button.save')) { const row = event.target.closest('tr'); console.log(row.innerText); } });
 <table class="table" id="myTable"> <thead> <tr> <th>Trails</th> <th>Province</th> <th>Country</th> </tr> </thead> <tbody> <tr> <td class="tableRow">Lion's Head</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button class="save">Save</button></td> </tr> <tr> <td class="tableRow">Pipe Track</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button class="save">Save</button></td> </tr> <tr> <td class="tableRow">Skeleton Gorge</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button class="save">Save</button></td> </tr> <tr> <td class="tableRow">Table Mountain</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button class="save">Save</button></td> </tr> <tr> <td class="tableRow">King Fisher Trail</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button button class="save">Save</button></td> </tr> <tr> <td class="tableRow">Robberg Peninsula</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button class="save">Save</button></td> </tr> <tr> <td class="tableRow">Diep Walle Forest Walk</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button class="save">Save</button></td> </tr> </tbody> </table>

You can't give same id's to different elements in DOM but you can create your custom attributes.您不能为 DOM 中的不同元素提供相同的 id,但您可以创建自定义属性。 I created button-id for each button.我为每个按钮创建了button-id Then used it to get the row that is clicked.然后用它来获取被点击的行。

 const save = document.querySelectorAll(".save"); // select all the buttons const rows = document.querySelectorAll("tr") // select all the rows console.log(rows) save.forEach((btn) => { btn.addEventListener("click", (e) => { let id = e.target.getAttribute('button-id') // get the clicked buttons id console.log(id) console.log(rows[id]) // with id select the row }); })
 <table class="table"> <tr> <th>Trails</th> <th>Province</th> <th>Country</th> </tr> <tr> <td class="tableRow">Lion's Head</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button button-id="1" class="save">Save</button></td> </tr> <tr> <td class="tableRow">Pipe Track</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button button-id="2" class="save">Save</button></td> </tr> <tr> <td class="tableRow">Skeleton Gorge</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button button-id="3" class="save">Save</button></td> </tr> <tr> <td class="tableRow">Table Mountain</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button button-id="4" class="save">Save</button></td> </tr> <tr> <td class="tableRow">King Fisher Trail</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button button-id="5" button class="save">Save</button></td> </tr> <tr> <td class="tableRow">Robberg Peninsula</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button button-id="6" class="save">Save</button></td> </tr> <tr> <td class="tableRow">Diep Walle Forest Walk</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button button-id="7" class="save">Save</button></td> </tr> </table>

First of all you have to assign the eventListener to all save buttons.首先,您必须将 eventListener 分配给所有保存按钮。 After trigger by a click you have access to it by using the event.target function.通过单击触发后,您可以使用 event.target 函数访问它。 Then you can go upwards to the parent element tr .然后你可以向上到父元素tr Afterwards you can handle the tr element.之后,您可以处理 tr 元素。

Note It would be better to use event.target.closest('tr') to avoid some collisions in the future.注意最好使用event.target.closest('tr')以避免将来发生一些冲突。 If same changes in the code will come.如果代码中会发生相同的更改。 See the comment from @connexo请参阅@connexo 的评论

 const save = document.querySelectorAll(".save"); save.forEach(s => { s.addEventListener("click", (e) => { const row = e.target.parentNode.parentNode; row.classList.toggle('border'); console.log(row) // }); });
 .border { background: red; }
 <table class="table"> <tr> <th>Trails</th> <th>Province</th> <th>Country</th> </tr> <tr> <td class="tableRow">Lion's Head</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button id="save" class="save">Save</button></td> </tr> <tr> <td class="tableRow">Pipe Track</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button id="save" class="save">Save</button></td> </tr> <tr> <td class="tableRow">Skeleton Gorge</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button id="save" class="save">Save</button></td> </tr> <tr> <td class="tableRow">Table Mountain</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button id="save" class="save">Save</button></td> </tr> <tr> <td class="tableRow">King Fisher Trail</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button id="save" button class="save">Save</button></td> </tr> <tr> <td class="tableRow">Robberg Peninsula</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button id="save" class="save">Save</button></td> </tr> <tr> <td class="tableRow">Diep Walle Forest Walk</td> <td class="tableRow">Western Cape</td> <td class="tableRow">South Africa<button id="save" class="save">Save</button></td> </tr> </table>

暂无
暂无

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

相关问题 如何创建一个HTML表,每行的列数未知,如何使列对齐,并为每个单元格/行设置一个ID? - How can I create an html table, with unknown number of columns per row, get the columns to be aligned, and have an id for each cell/row? 我做了一个带有可编辑单元格的HTML表。 当我编辑单元格时,我需要该行及其下的行来重新计算 - I've made an HTML table with editable cells. When I edit a cell I need the row and the rows below it to recalculate 如何使用 vuejs 定位表格单元格中的元素? - How can I target elements in a table cell using vuejs? 我需要仅使用 javascript 更新 html 表中的特定单元格,我如何定位此单元格? - I need to update a specific cell in an html table using only javascript, how do i target this cell? 如何将数组复制到从某个单元格开始的列的每个单元格,而不是复制到行的每个单元格? - How can I copy an array to each cell of a column starting from a certain cell, instead of to each cell of a row? 如何获得被点击的表格行的TBODY? - How can I get a clicked table row's TBODY? 如何将HTML表格列中的每个单元格转换为引用本地zip文件的可下载链接? - How can I convert each cell in HTML table column to a downloadable link, that references a local zip file? 从HTML表格删除一行后,如何存储单元格的值? - How can I store the value of a cell upon deleting a row from an HTML table? 我找不到在html表格的单元格上单击过的元素 - I can't find which element was clicked on an html table's cell 如何根据单元格单击突出显示表格行? - How can I highlight a table row based on a cell click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM