【发布时间】:2018-01-16 22:45:15
【问题描述】:
我有几个不同的带有图片说明的图片库。我正在尝试将相应图像的下载链接附加到每个图库标题的末尾。下载链接需要基于每张图片的img src。
我可以将下载链接附加到标题中。但是,它还在每个图像标题上为图库中包含的每个图像附加了一个下载图标。而不是每张图片一个下载链接。
HTML
<div>
<figure class="gallery-item">
<!-- BoGalleryImg -->
<div class="gallery-icon landscape">
<a href="http://creative.dev.j2noc.com/thehub/wp-content/uploads/2017/10/crkis-2515.jpg" data-rel="lightbox-gallery-1">
<img width="300" height="250" src="http://creative.dev.j2noc.com/thehub/wp-content/uploads/2017/10/crkis-2515.jpg" class="attachment-full size-full" alt="" aria-describedby="gallery-1-823">
</a>
</div>
<!-- EoGalleryImg -->
<!-- BoCaption -->
<figcaption class="wp-caption-text gallery-caption" id="gallery-1-823">
<a target="_blank" href="https://jira.j2noc.com/jira/browse/CRKIS-2515">
CRKIS-2515
</a>
</figcaption>
<!-- EoCaption -->
</figure>
<br />
<figure class="gallery-item">
<!-- BoGalleryImg -->
<div class="gallery-icon landscape">
<a href="http://creative.dev.j2noc.com/thehub/wp-content/uploads/2017/10/crkis-2379b.jpg" data-rel="lightbox-gallery-1"><img width="300" height="250" src="http://creative.dev.j2noc.com/thehub/wp-content/uploads/2017/10/crkis-2379b.jpg" class="attachment-full size-full" alt="" aria-describedby="gallery-1-817"></a>
</div>
<!-- EoGalleryImg -->
<!-- BoCaption -->
<figcaption class="wp-caption-text gallery-caption" id="gallery-1-817">
<a target="_blank" href="https://jira.j2noc.com/jira/browse/CRKIS-2379B">
CRKIS-2379B
</a>
</figcaption>
<!-- EoCaption -->
</figure>
</div>
jQuery
$(document).ready(function() {
$('.attachment-full').each(function(index, element){
// create variable from img src
var imgSrc = $(element).attr('src');
console.log(imgSrc);
//Append Download Link to Caption
$('.gallery-caption').append('<a href="'+imgSrc+'" download><span class="glyphicon glyphicon-download-alt icon-download"></span></a>');
});
});
这几天我一直在尝试解决这个问题,但我已经很接近了,我只是不知道如何让它只为相应的图像附加一个下载图标。提前感谢您提供的任何帮助。
【问题讨论】:
-
我无法准确说出您想要什么。您是否尝试在 Jquery 的最后一行将“$('.gallery-caption').append(..." 更改为 "$(this).append(..."?
-
是的,控制论是正确的。
-
我尝试了@Cybernetic 的建议,但没有达到我希望的效果。如果您查看我的 CodePen 链接,它说明了当前正在发生的事情。请注意,每个图像标题有两个下载图标,而应该只有一个。
-
它适用于正确的图像 url(你的不起作用)。将此链接放在您的两个图像源 (encrypted-tbn0.gstatic.com/…) 中,您将获得预期的行为。
-
在此处查看结果:gph.is/2B7otdc
标签: javascript jquery html css image