我想遍历表 id="myTable"中每一行的每个单元格,并获取单元格的子 div 的 ID。
到目前为止,我有类似的东西:
$("td.theCell").each(function() {
var id = $(this).attr('id'); //this would give me the td id but I want its child div's id
});
我的 html 是这样的:
<table id='myTable'>
<tr>foo</tr>
<tr>bar</tr>
<td>foo</td>
<td class='theCell'>
<div id='myDiv'>foo</div>
</td>
<tr>foo</tr>
<tr>bar</tr>
</table>
有人知道这样做的好方法吗?提前致谢。
最佳答案
试试这个
$("td.theCell").each(function() {
var id = $(this).find('div').attr('id');
});
您需要正确获取 div 的 Id 而不是 td.thecell 元素。
所以你需要使用.find()
来选择里面的dive..
关于javascript - 使用 jquery 遍历 html 表行以获取 <td> 单元格内的 div id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12846973/