简体   繁体   English

Javascript - 动态获取行表的 id

[英]Javascript - Get id of row table dynamically

i have a function in JavaScript for detecting every object clicked.我在 JavaScript 中有一个 function 用于检测每个 object 点击。

document.addEventListener("click", reply_click);
function reply_click(evt){
    evt = evt || window.event;
    evt = evt.target || evt.srcElement;
    alert(evt.id);
}

and i have html code that contain table and every row have an id.我有 html 代码,其中包含表格,每一行都有一个 ID。 the html code is: html 代码为:

<table id='tableid1'>
<tr id="rowid1" class='handz'>
   <td id='tdid1'><font id='fontid1'>hello</font></td>
</tr>
</table>

i want a method (not onclick event for tr) to when click on table row or table td or text inside td that be able to detect tr id?我想要一种方法(不是 tr 的 onclick 事件)当单击表格行或表格 td 或 td 内的文本时能够检测到 tr id? any help or suggesting will be appreciated.任何帮助或建议将不胜感激。

This could be a solution:这可能是一个解决方案:

 document.addEventListener("click", reply_click); function reply_click(evt) { evt = evt || window.event; evt = evt.target || evt.srcElement; let rowParent = evt.parentElement while (rowParent.tagName.== 'TR') { rowParent = rowParent.parentElement } console.log(rowParent.id) }
 <table id='tableid1'> <tr id="rowid1" class='handz'> <td id='tdid1'> <font id='fontid1'>hello1</font> </td> </tr> <tr id="rowid2" class='handz'> <td id='tdid2'> <font id='fontid2'>hello2</font> </td> </tr> </table>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM