How to select second table structure under a div?

2019-08-12 21:24发布

问题:

I am trying to select every second table under a div.

so my html is like

<div class ='tableDiv'>
  <table class='table'>
    ....
  </table>

  <table class='table'>
    ....
  </table>

</div>

...other stuff...

<div class ='tableDiv'>
  <table class='table'>
    ....
  </table>

  <table class='table'>
    ....
  </table>
</div>

other stuff

<div class ='anotherTableDiv'>
  <table class='table'>
    ....
  </table>

  <table class='table'>
    ....
  </table>
</div>

I am trying to select every second table under 'tableDiv' div.

I tried.

$('.tableDiv table:nth-child(even));

but it doesn't work. I can't really change class name or add an ID so it's a bit difficult. Can anyone gives me a hint for it? Thanks a lot!

回答1:

Use jQuery's :even selector.

$('.tableDiv table:even')


回答2:

Make sure you close your quote marks. What you started with is fine except for that.