javascript jquery选择嵌套的div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14267958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:11:04  来源:igfitidea点击:

jquery select nested div

javascriptjqueryhtmlcss

提问by Athapali

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

如何选择包含“我的内容”的 div?

<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?

该 div 与 td.ms-disc-bordered-noleft 有什么关系?

回答by Jeromy French

  • $('.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");
  • $('.ms-disc-bordered-noleft div:last')
  • $('.ms-disc-bordered-noleft div:eq(2)')
  • $('.ms-disc-bordered-noleft').find('div:eq(2)');
  • 或者 $("div:contains('my content'):last");

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

返回机器人...我的意思是 div...你正在寻找。

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

我有一种感觉,这$('.ms-disc-bordered-noleft div:last')是你最好的选择;我的性能测试表明它通常是 4 个建议中最快的(FireFox 更喜欢$('.ms-disc-bordered-noleft').find('div:eq(2)');)。

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

有关正在使用的不同选择器的示例,请参阅http://jsfiddle.net/jhfrench/cfeZU



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

至于您问题的第二部分,该 div 是td.ms-disc-bordered-noleft元素的“后代” 。更具体地说,它是 td 的孩子的孩子。

回答by VisioN

One option as second child of inner <div>:

作为inner的第二个孩子的一种选择<div>

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

回答by Roko C. Buljan

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

如何选择包含“我的内容”的 div?

$('.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

回答by Rob

It's a parent node, you can traverse up an down using .parent()and .children()or .find()

它是一个父节点,您可以使用.parent().children().find()

回答by Matthew Davis

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.

就与 td 的关系而言,您可以从上述开始,然后返回。

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.

就性能而言,这些不一定是最快的选择,但它们是最短的代码。我喜欢短代码。

回答by Devin McQueeney

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

or

或者

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