【问题标题】:Create download link based on img src Jquery基于img src jquery创建下载链接
【发布时间】: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>'); 
    }); 
});

这几天我一直在尝试解决这个问题,但我已经很接近了,我只是不知道如何让它只为相应的图像附加一个下载图标。提前感谢您提供的任何帮助。

我的CODEPEN DEMO

【问题讨论】:

  • 我无法准确说出您想要什么。您是否尝试在 Jquery 的最后一行将“$('.gallery-caption').append(..." 更改为 "$(this).append(..."?
  • 是的,控制论是正确的。
  • 我尝试了@Cyber​​netic 的建议,但没有达到我希望的效果。如果您查看我的 CodePen 链接,它说明了当前正在发生的事情。请注意,每个图像标题有两个下载图标,而应该只有一个。
  • 它适用于正确的图像 url(你的不起作用)。将此链接放在您的两个图像源 (encrypted-tbn0.gstatic.com/…) 中,您将获得预期的行为。
  • 在此处查看结果:gph.is/2B7otdc

标签: javascript jquery html css image


【解决方案1】:

您的 html 解析起来有点麻烦。下面是一些 jQuery,它可以使用所需的图标数重现您的输出:

images = ['http://srsounds.com/j3/images/easyblog_articles/2943/han_solo.jpg', 'https://lumiere-a.akamaihd.net/v1/images/chewie-db_2c0efea2.jpeg?region=0%2C154%2C1592%2C796'];
labels = ['CRKIS-2515', 'CRKIS-2379B']

images.forEach(function(value, index) {
    $('body').append("<img src=" + value + " width=350><br>" + labels[index] + " <i class='fa fa-download' aria-hidden='true'></i><br><br>");
    $('body').css('color', 'steelblue'); 
})

如果您想使用这些图标,请使用很棒的字体:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

您的画廊布局可能还有其他要求,但希望这会帮助您开始采用更简洁的方法。

如果您在为图标添加链接属性或实现其他布局时遇到问题,请告诉我。

********编辑********

如果您想/需要坚持原来的方法,请将您的 jQuery 更改为:

  $(document).ready(function() {
  download_ids_hold = [];
  imgSrc_hold = []; 
  $('.attachment-full').each(function(index, element){ 
      // create variable from img src
      var imgSrc = $(element).attr('src');
      imgSrc_hold.push(imgSrc)
      console.log(imgSrc); 

      //Append Download Link to Caption
      $('.gallery-caption').each(function(ind_2, elem_2) { 
      add_id = 'a_' + Math.floor((Math.random() * 100000) + 1);
      download_ids_hold.push(add_id)
      if(index == 0) { 
        $(this).append('<a id=' + add_id + ' download><i class="fa fa-download" aria-hidden="true"></i></span></a>'); 
      }
      })
   });
   for(id=0;id<download_ids_hold.length;id++) { $('#' + download_ids_hold[id]).attr('href', imgSrc_hold[id]) }
});

我们跟踪图像来源和下载 ID,并在最后适当地使用它们。

【讨论】:

  • 这是有道理的。但是,出于我的目的,这将不起作用。我正在使用 WordPress,并且我有几个不同的画廊正在动态填充。因此,我将无法像您一样创建可用图像的数组。我需要 JQuery 首先从页面创建一个可用图像源的数组,然后将它们应用于我试图附加到标题的 html 的 href。
  • 我添加了一个 EDIT 以使其适用于您的方法。您只需遍历每个 .gallery-caption 并检查您的原始索引(来自 .attachment-full 迭代)是否为 0。我为图标使用了很棒的字体,因此如果需要,请更改为您的引导字形。跨度>
  • 是的,我也有类似的想法,这适用于为每个图像创建一个下载按钮。但是,下载链接始终使用图库中的第一个 img src。有没有办法强制它使用下载链接对应的图像?即 Han 下载 Han 和 Chewie 下载 Chewie。我稍微清理了我的代码并实现了您的解决方案on this codepen感谢您对所有这些的帮助。
  • 用新脚本更改了 EDIT 脚本。试试看。
  • 成功了!解决方案比我意识到的要复杂得多。我非常感谢您抽出时间来帮助我。我还有很多东西要学。
猜你喜欢
  • 2011-06-12
  • 1970-01-01
  • 2010-12-22
  • 1970-01-01
  • 2016-08-01
  • 2016-10-25
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多