Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery查找直系子代,不再继续_Javascript_Jquery - Fatal编程技术网

Javascript 使用jQuery查找直系子代,不再继续

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.

搜索特定元素时,如何获取直接子元素?例如,我想获取表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.
                                 // 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;