jQuery只选择主表中的tr/td,而不是嵌套表.

The*_*ear 16 jquery

我目前有一个带有嵌套表的表:

   <table class="datagrid" id="report">
       <thead>
         <tr>
          <th>item1</th>
          <th>item2</th>
         </tr>
       </thead>
       <tbody>
       <tr class="odd"> <-- on click from this level only 
        <td></td>  
        <td></td>
       </tr>
       <tr>
        <td colspan="2">
         <table>
          <tr class="odd"> <-- not to be fired here
           <td></td> 
Run Code Online (Sandbox Code Playgroud)

等表结构.

我目前正在使用以下jQuery,tr无论表级别如何,它都会触发.如何将其更改为仅在第一级触发?

 $(document).ready(function(){
            $("#report tr:not(.odd)").hide();
            $("#report tr:first-child").show();

            $("#report tr.odd").click(function(){
                $(this).next("#report tr").fadeToggle(600);
            });
        });
Run Code Online (Sandbox Code Playgroud)

Gum*_*mbo 29

使用子选择器>仅选择那些tr作为报表表子项的元素,tbody例如:

$("#report > tbody > tr.odd")
Run Code Online (Sandbox Code Playgroud)


ner*_*ess 10

使用此解决方案,您之间是否有tbody标记无关紧要:

$('table').find('tr:first').parent().children()
Run Code Online (Sandbox Code Playgroud)

  • 正确的理解是,一旦找到第一个`tr`标签,那么就采用它的父节点,然后返回其中的所有子节点.因此,将返回所选`table`的所有`tr`元素 (3认同)