Javascript 使用jQuery查找直系子代,不再继续
搜索特定元素时,如何获取直接子元素?例如,我想获取表t1的tr元素 然而,我数到了4,我不明白为什么 是一个完整的示例:使用:Javascript 使用jQuery查找直系子代,不再继续,javascript,jquery,Javascript,Jquery,搜索特定元素时,如何获取直接子元素?例如,我想获取表t1的tr元素 然而,我数到了4,我不明白为什么 是一个完整的示例:使用: 'Count = ' + $('#t1 > tbody').children('tr').length; // or: $("#t1 > tbody > tr").length // or: $("#t1")[0].rows.length; // In this case, equal to previous code.
'Count = ' + $('#t1 > tbody').children('tr').length;
// or: $("#t1 > tbody > tr").length
// or: $("#t1")[0].rows.length; // In this case, equal to previous code.
// Warning: This also includes the rows from
// the <thead> and <tfoot> sections.
使用:
这是因为使用$'t1 tbody'可以从两个表中获得tbody
你可以直接使用
这里是您更新的示例:这是因为使用$'t1 tbody'可以从两个表中获得tbody
你可以直接使用
下面是您更新的示例:Ah!我试着小心trs,因为我忘了尸体。太好了,浪费了一个小时。谢谢你,罗布!啊!!我试着小心trs,因为我忘了尸体。太好了,浪费了一个小时。谢谢你,罗布!
'Count = ' + $('#t1 tbody').children('tr').length;
'Count = ' + $('#t1 > tbody').children('tr').length;
// or: $("#t1 > tbody > tr").length
// or: $("#t1")[0].rows.length; // In this case, equal to previous code.
// Warning: This also includes the rows from
// the <thead> and <tfoot> sections.
<table id="t1" bgcolor="yellow"> <-- #t1
<tbody> <--- tbody
<tr> ... </tr> <----- Child 1
<tr> ... <----- Child 2
<tbody> <--- tbody (unexpected?)
<tr> ... </tr> <----- Child 3
<tr> ... </tr> <----- Child 4
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
$('#t1 > tbody > tr').length;