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