使用JQuery的find方法,如何基于子元素“ foo”的内容来选择某些HTML元素“ foo”的同级?

问题描述 投票:0回答:1

假设我具有如下所示的HTML:

<div class="foo-container">
  <div class="foo">
    <div class="foo-child">
      <span>The Important Label</span>
    </div>
  </div>
  <div class="elem-to-manipulate">
    <p>I want to manipulate the style in this because it's the sibling of "The Important Label"</p>
  </div>
</div>

基于同级具有文本p的事实,我该如何定位该The Important Label元素。

我有类似的经历

$('.root').find('.foo > .foo-child > span:contains("The Important Label")')

成功使我获得span元素。但是现在如何在同级p中获取div元素?有什么方法可以告诉找到哪一个父母要得到兄弟姐妹吗?

jquery html jquery-selectors
1个回答
0
投票
[+匹配先前匹配元素的以下同级元素
((取决于您的用例,您可能会对.foo:contains("The Important Label") + div > p ...感到满意)
© www.soinside.com 2019 - 2024. All rights reserved.