我正在尝试更改h2或h3元素之后的第一个p元素,无论是否存在其他元素。
所以这个html是
<h3>This is the subhead in use</h3>
<img src="http://placehold.it/350x150"/>
<p>Here is some lipsum orum kind of textHere is some lipsum orum kind of textHere is some lipsum orum kind of textHere is some lipsum orum kind of textHere is some lipsum orum kind of text</p>
<p>Here is some lipsum orum kind of textHere is some lipsum orum kind of textHere is some lipsum orum kind of textHere is some lipsum orum kind of textHere is some lipsum orum kind of text</p>
<h3>This is the subhead in use</h3>
<p>Here is some lipsum orum kind of text</p>
<aside>
<h2>This is the aside title</h2>
<p>Here is some more lipsum orem. Here is some more lipsum orem. Here is some more lipsum orem. Here is some more lipsum orem.</p>
</aside>
和一些jquery(2.13)
$(function(){
$('h3, h2').next('p').css({ "color":"green"});
});
因此,这会在h3或h3标记之后找到第一个p,但如果中间还有另一个元素则找不到它。并不总是,但有时我会想要一个img,div或figure元素和h2或h3,但仍然希望jquery找到第一个p(在上面的代码中,它失败了)
我已经尝试了许多选择器和方法的组合,包括p:first-of-type,siblings,nextAll等。我想这样做而不需要在html部分的第一个p元素中添加一个类。< / p>
请帮忙。
答案 0 :(得分:5)
它没有按预期工作,因为.next('p')
只会选择紧随其后的FragmentManager
元素。您可以使用.nextAll()
method选择所有以下p
元素(不仅仅是紧随其后的元素),然后链接p
方法以获取集合中的第一个匹配项:
.first()