我将为这个页面上的每个表(大约 25
个不同的表)设置一次这个按钮,我希望找到一种方法来调用 onclick
事件函数 constructDataValueString
并传入父表的 id
(这样我就不必显式引用表 ID)。
有没有办法这样写:
onclick="constructDataValueString(*id_of_parent_table*)"
HTML 标记的图形示例:
最佳答案
您可以使用 closest() ,例如:
onclick="constructDataValueString(this.closest('table').id)"
示例
const constructDataValueString = (id) => console.log("table id is: ", id);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:50% !important;}
<table id="populationbyage" border="2">
<thead>TABLE</thead>
<tbody>
<tr>
<td>
Some Cell...
</td>
<td>
<button onclick="constructDataValueString(this.closest('table').id)">
Copy
</button>
</td>
</tr>
</tbody>
</table>
关于javascript - 如何在onclick事件中使用父表的id作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319503/