简体   繁体   中英

jquery select nested div

How do I select div that contains "my content"?

<table>
  <tr>
    <td class="ms-disc-bordered-noleft">
      <div class="">
        <div>some content</div>
        <div>my content</div>
      </div>
    </td>
  </tr>
</table>

what relation does that div has with td.ms-disc-bordered-noleft?

  • $('.ms-disc-bordered-noleft div:last')
  • $('.ms-disc-bordered-noleft div:eq(2)')
  • $('.ms-disc-bordered-noleft').find('div:eq(2)');
  • or $("div:contains('my content'):last");

return the droid...I mean div...you are looking for.

I have a feeling that $('.ms-disc-bordered-noleft div:last') is your best option; my performance test shows that it is generally the fastest of the 4 proposals (FireFox prefers $('.ms-disc-bordered-noleft').find('div:eq(2)'); ).

See http://jsfiddle.net/jhfrench/cfeZU for examples of the different selectors in use.


As for the second part of your question, that div is a 'descendant' of the td.ms-disc-bordered-noleft element. More specifically, it is the child of the td's child.

作为内部<div>第二个孩子的一个选项:

var div = $(".ms-disc-bordered-noleft > div > div:eq(1)");

How do I select div that contains "my content"?

$('.ms-disc-bordered-noleft').find('div').contains("my content");

IF I did not understand well your Q. ...you can use:

$('.ms-disc-bordered-noleft div').find('div:last');

        ^^-parent         ^^-first children  ^^-last div

这是一个父节点,您可以使用遍历了一个向下.parent().children().find()

Most direct route would be:

var div = $("div:contains('my content'):last");

As far as the relationship to the td goes, you could start with the above and work your way back.

var cell = div.closest('td');

These aren't necessarily the fastest options in terms of performance, but they are the shortest code. And I like short code.

.ms-disc-bordered-noleft div div:nth-child(2)

要么

.ms-disc-bordered-noleft div div:last-child

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM