我有一个动态创建的表:
<div id="gameListDiv">
<table id="gameListTable">
<tr id="1">
<td>A</td>
<td>B</td>
<td class="tpButton">C</td>
</tr>
<tr id="2">
<td>D</td>
<td>E</td>
<td class="tpButton">F</td>
</tr>
</table>
</div>
我有一个听众:
$('#gameListDiv').on('click','.tpButton', function(toggleThisTP) {
// If user clicks C, return the row ID of '1'
// if user clicks F, return the row ID Of '2'
});
如上面的代码注释,当用户点击表格第3列的单元格时,如何获取特定行的ID标签的值?
最佳答案
这应该有效:
$('#gameListDiv').on('click','.tpButton', function() {
var id = jQuery(this).closest('tr').attr('id');
alert(id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="gameListDiv">
<table id="gameListTable">
<tr id="1">
<td>A</td>
<td>B</td>
<td class="tpButton">C</td>
</tr>
<tr id="2">
<td>D</td>
<td>E</td>
<td class="tpButton">F</td>
</tr>
</table>
</div>
关于javascript - 如何从单击的 TD 元素的 TR 中获取 ID 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30783443/