开发者

jQuery addClass to second Table

开发者 https://www.devze.com 2023-03-01 02:36 出处:网络
I must be loosing it but I cannot addClass to the second table (with id \'table2\') similar to the code below.In my current code the tables are NOT labeled with ids, I just did it to show you which ta

I must be loosing it but I cannot addClass to the second table (with id 'table2') similar to the code below. In my current code the tables are NOT labeled with ids, I just did it to show you which table I need to add a class to.

<div id="content_area">
  <table id="table1">
    <tr>
      <td>data</td>
      <td>
        <table><tr><td>data</td></tr></table>
        <table><tr><td>data</td></tr></table>
      </td>
      <td>
        <table><tr><td>data</td></tr></table>
      </td>
    </tr>
  </table>
  <table id="table2"><tr><td>data</td></tr></table>
</div>

$('#conte开发者_JAVA百科nt_area').find('table:eq(1)').addClass('stuff');


This is what worked for me:

$('#content_area').children('table:eq(1)').addClass('stuff');

This requires the fewest changes to your existing code.


What about

$('#content_area > table:nth-child(2)').addClass('stuff');


Try:

$('#content_area table').eq(4).addClass('stuff');

In your example, the table with id="table2" is actually the 5th table inside of content_area. Start counting every <table> tag.

In case of dynamic # of tables within table 1 you should probably use:

$('#content_area').children('table:eq(1)').addClass('stuff');

This will look at the direct children of content_area and get the 2nd table from those children. [Per Kevin Buchan's answer]

See: http://jsfiddle.net/tAZht/2/

0

精彩评论

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