如何防止jQuery选择器包含嵌套元素?

时间:2021-05-16 14:30:53

I'm new to jQuery so hopefully there is an easy answer to this.

我是jQuery新手,希望有一个简单的答案。

I have html similar to:

我有类似于:

<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(在嵌套表中包含)。我的问题是:如何防止第三个被选中?

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类添加到最后一行,并将其从结果中排除,但我更希望有一个选项允许我控制select引擎搜索的深度。

1 个解决方案

#1


15  

I believe the syntax:

我认为语法:

$("#dataTable > tr").length

mean "just tr's on the next level".

意思是“下一个水平的tr”。

#1


15  

I believe the syntax:

我认为语法:

$("#dataTable > tr").length

mean "just tr's on the next level".

意思是“下一个水平的tr”。