Dan*_*n A 0 javascript jquery jquery-selectors
我有一个动态创建的表:
<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>
Run Code Online (Sandbox Code Playgroud)
我有一个听众:
$('#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'
});
Run Code Online (Sandbox Code Playgroud)
与上面的代码注释一样,当用户单击表的第3列中的单元格时,如何获取特定行的ID标记的值?
这应该工作:
$('#gameListDiv').on('click','.tpButton', function() {
var id = jQuery(this).closest('tr').attr('id');
alert(id);
});Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1586 次 |
| 最近记录: |