jQuery - 尚未被锚包围的环绕声图像

问题描述 投票:1回答:2

我在填写内容丰富的网站时遇到了问题。我们在内容中的所有图像上设置了一个灯箱。有意地在图像上设置了“灯箱”类,并且没有在图像周围创建链接。

所以我发现我可以使用jQuerys wrap()函数在内容区域的每个图像周围添加lightbox-image链接。可悲的是,它还包装已经被锚点包围的图像(例如PDF-Links)并覆盖它们。

我如何告诉jQuery只包含尚未包含锚标签的内容中的图像?

我现在有以下内容:

<script type="text/javascript">
$(document).ready(function($){
$('article img').each(function(){
if ($('article img').hasClass('lightbox')){
$(this).removeAttr('class');
}
$(this).wrap('<a href="'+this.src.replace("http://www.mydomain.com","")+'" class="lightbox" rel="group1"></a>');
});
$('.lightbox').lightbox();
});
</script>

所以我从图像中删除灯箱类,并在每个图像周围添加一个锚点,其中src与图像相同(没有缩略图)。

javascript jquery html anchor
2个回答
2
投票

LIVE DEMO

$('article img').each(function(){
  if( this.parentNode.tagName != 'A' ){
      $(this).removeClass('lightbox')
             .wrap('<a href="'+this.src.replace("http://www.mydomain.com","")+'" class="lightbox" rel="group1"></a>');
  }
});

0
投票

更改您的选择器

$('article>img').each(function(){
© www.soinside.com 2019 - 2024. All rights reserved.