我真的很难在jQuery中定位一个元素,并希望看到它是否可能有所帮助。
我的HTML是这样的结构...
<p>Slimming World Nutritionist Jenny Allan says: “Ban the ‘D’ word. It has so many negative associations for most people, making them think of being deprived and not being able to share meals with family and friends and enjoy social events.”</p>
<p style="text-align: center;">
<div class="pinit aligncenter" data-indexer="1">
<div class="img-caption">Lose weight and feel great a your wedding!</div>
<div class="clear"></div>
<noscript><img class="aligncenter size-full wp-image-33687" title="Lose weight and feel great a your wedding!" alt="danielle-david-real-wedding-05" src="http://www.giraffetest.co.uk/wp-content/uploads/2013/05/danielle-david-real-wedding-05.jpg" width="650" height="433" /></noscript>
</p>
<h2>Myth 4 – Healthy eating is expensive</h2>
<div data-indexer="2"></div>
<p>“Your weekly shop will go further, your overall food bills won’t go up – and neither will your weight.”</p>`
如果父级(在本例中为
标签)后面紧跟着h2标签,我想选择一个div内的所有图像 - 在本例中
我已经尝试过以下但是它无法正常工作: - (
jQuery(".pinit").parent().next("h2").css("marginBottom", "0");
有人可以帮忙吗?
谢谢,
詹姆斯
答案 0 :(得分:0)
你可以这样做:
jQuery(".pinit").each(function() {
if (jQuery(this).parent().next("h2").length) {
jQuery(this).children("img").css("marginBottom", "0");
}
});
答案 1 :(得分:0)
jQuery(".pinit").filter(function(){
return jQuery(this).parent().next("h2").length > 0;
}).css("marginBottom", "0");
基本上filter
用于将.pinit
元素集合过滤到仅包含父元素后跟h2
的元素。然后,其余的.pinit
元素已应用css
。