假设我具有如下所示的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
元素?有什么方法可以告诉找到哪一个父母要得到兄弟姐妹吗?
+
匹配先前匹配元素的以下同级元素((取决于您的用例,您可能会对.foo:contains("The Important Label") + div > p
...感到满意)