开发者

jQuery addClass to second Table

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜