简体   繁体   中英

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.

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:

$("#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?

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.

I believe the syntax:

$("#dataTable > tr").length

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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