开发者

jQuery select only tr/td in main table, not nested tables.

开发者 https://www.devze.com 2022-12-21 19:14 出处:网络
I currently have a table with a nested table in it: <table class=\"datagrid\" id=\"report\"> <thead>

I currently have a table with a nested table in it:

   <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 cl开发者_StackOverflow中文版ass="odd"> <-- not to be fired here
           <td></td> 

etc table structure.

I am currently using the following jQuery, which fires on every tr regardless of the table level. How do I change it to only fire on the first level?

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

            $("#report tr.odd").click(function(){
                $(this).next("#report tr").fadeToggle(600);
            });
        });


Use the child selector > to only select those tr elements that are a child of the report table’s tbody, for example:

$("#report > tbody > tr.odd")


with this solution it does not matter whether you have a tbody tag in between:

$('table').find('tr:first').parent().children()


You use the > selector to target only the direct descendant of an element. You have to target the implicit tbody element inside the table also:

$('#report>tbody>tr.odd')


You want

$("#report>tr")

The > means direct descendant (child) rather than any descendant.

0

精彩评论

暂无评论...
验证码 换一张
取 消