<分区>
当且仅当它的相邻兄弟是某个元素时,我想选择一个元素。
<div>
<h1>Select Me</h1>
<h2>I'm the sibling, yay!</h2>
<div>
<p>A paragraph.</p>
</div>
</div>
<div>
<h1>Don't Select Me - I have no h2 sibling. :(</h1>
<div>
<p>A longer paragraph.</p>
</div>
</div>
在这个特定示例中,我只想选择第一个 h1
,因为它有一个相邻的 h2
。另一个 h1
不符合该条件,所以我不想选择它。
我找到了 this但它对我不起作用,因为该元素有其他 sibling ,而且永远不是独生子。