0

我真的很难在 jQuery 中定位一个元素,并且希望能得到一些帮助,看看它是否可能。

我的 HTML 是这样的结构……</p>

<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.”&lt;/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.”&lt;/p>`

如果父级(在本例中为

tag) 后面紧跟着一个 h2 标签——在这个例子中就是这样。

我已经尝试了以下但它不工作:-(

jQuery(".pinit").parent().next("h2").css("marginBottom", "0");

有人可以帮忙吗?

谢谢,

詹姆士

4

2 回答 2

0

你可以做:

jQuery(".pinit").each(function() {
    if (jQuery(this).parent().next("h2").length) {
        jQuery(this).children("img").css("marginBottom", "0");
    }
});
于 2013-06-18T14:27:57.343 回答
0
jQuery(".pinit").filter(function(){
    return jQuery(this).parent().next("h2").length > 0;
}).css("marginBottom", "0");

基本上filter用于将.pinit元素集过滤到仅具有父元素后跟h2. 然后应用了其余.pinit元素css

于 2013-06-18T14:28:48.897 回答