简体   繁体   English

如何防止jQuery Selector包含嵌套元素?

[英]How do I prevent a jQuery Selector from including nested elements?

I'm new to jQuery so hopefully there is an easy answer to this. 我是jQuery的新手,所以希望有一个简单的答案。

I have html similar to: 我的html类似于:

<table id="dataTable">
    <tr> <!-- I want this row -->
        <td>...</td>
    <tr>
    <tr>
        <td>
           <table>
               <tr> <!-- I do not want this row -->
                   <td>...</td>
               </tr>
           </table>
        </td>
    <tr>
</table>

I am using jQuery similar to this: 我使用的jQuery类似于:

$("#dataTable tr").length;

I would expect length to equal 2, but its returning 3 (includes the <tr> in the nested table.) My question is: How do I prevent the 3rd <tr> from being selected? 我希望长度等于2,但它返回3(包括嵌套表中的<tr>。)我的问题是:如何防止第3个<tr>被选中?

I know that I could add an ignorethisrow class to the last row and exclude that from my results, but I'd prefer an option that allows me to control the depth that the select engine searches. 我知道我可以在最后一行添加一个ignorethisrow类并从我的结果中排除它,但我更喜欢一个选项,它允许我控制选择引擎搜索的深度

I believe the syntax: 我相信语法:

$("#dataTable > tr").length

mean "just tr's on the next level". 意思是“只是在下一个层次上”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM