开发者

jQuery select image in div if image parent does't have a certain class

开发者 https://www.devze.com 2022-12-25 02:07 出处:网络
Wordpress wraps images with captions in a div with a class of .wp-caption. I\'m looking for a way to select images that don\'t开发者_运维百科 have this div so I can wrap them in different div. (to ke

Wordpress wraps images with captions in a div with a class of .wp-caption. I'm looking for a way to select images that don't开发者_运维百科 have this div so I can wrap them in different div. (to keep a consistent border around all the images)

<div class="blog-post-content"> 
 <div id="attachment_220" class="wp-caption alignleft" style="width: 310px">
  <a href="/somewhere/"><img class="size-medium wp-image-220" src="/path/to/image" alt="" width="300" height="280" /></a>
  <p class="wp-caption-text">Caption Text</p>
 </div>
 <p>This is the body of the post</p>
</div>

To test my selector, I'm just trying to add a green border. I can handle the .wrap() once the selector is working.

The most promising of my attempts is:

$('.blog-post-content img').parent('div:not(".wp-caption")').css('border', '2px solid green');

... but no luck.


How about this: (untested)

$('.blog-post-content img').filter(function(){
    return !$(this).parents('div').hasClass('wp-caption');
}).css('border', '2px solid green');


try:

$('.blog-post-content img').parent(':not("div.wp-caption")')

Not if what Matti says abotu the a element in the hierarchy then the above wont work.


I know this question was asked a long time ago, but I would like to suggest the following:

$('.blog-post-content img').closest('div:not(".wp-caption")')

Untested, but I think that should work, and is shorter than the answer above that works. Using closest means the a is ignored.

0

精彩评论

暂无评论...
验证码 换一张
取 消