简体   繁体   English

href内的输出变量-Javascript

[英]Output variable inside an href - Javascript

I am using Magnific Popup for my gallery. 我正在为画廊使用Magnific Popup。 When you click on a thumbnail, it popups the image. 单击缩略图时,它将弹出图像。 I am trying to add a "Download image" button inside the popup. 我正在尝试在弹出窗口中添加一个“下载图像”按钮。 I have the HTML snippet displaying fine, but the href of the download link needs to be populated with the img url. 我的HTML代码段显示正常,但是需要使用img url填充下载链接的href。

Here is the JS code: 这是JS代码:

markup: '<div class="mfp-figure">'+
                '<div class="mfp-close"></div>'+
                '<div class="download-img"><a href="#">Download image</a></div>'+
                '<figure>'+
                    '<div class="mfp-img"></div>'+
                    '<figcaption>'+
                        '<div class="mfp-bottom-bar">'+
                            '<div class="mfp-title"></div>'+
                            '<div class="mfp-counter"></div>'+
                        '</div>'+
                    '</figcaption>'+
                '</figure>'+
            '</div>',

How do I put the image src variable in the anchor's href? 如何将图片src变量放入锚的href中?

If you check the Magnific Popup API you will see that it provides a callback for the event markupParse 如果您检查了Magnific Popup API,您将看到它为事件markupParse提供了回调。

So add this to your Magnific Popup options 因此,将其添加到您的Magnific Popup选项

callbacks: {
      markupParse: function(template, values, item) {
       template.find('.download-img a').prop('href',item.src);
      }
    }

Full demo at http://codepen.io/gpetrioli/pen/reoqQv 完整演示位于http://codepen.io/gpetrioli/pen/reoqQv

Just put the variable out of quotes. 只需将变量放在引号中即可。

markup: '<div class="mfp-figure">'+
                '<div class="mfp-close"></div>'+
                '<div class="download-img"><a href="' + yourVar + '">Download image</a></div>'+
                '<figure>'+
                    '<div class="mfp-img"></div>'+
                    '<figcaption>'+
                        '<div class="mfp-bottom-bar">'+
                            '<div class="mfp-title"></div>'+
                            '<div class="mfp-counter"></div>'+
                        '</div>'+
                    '</figcaption>'+
                '</figure>'+
            '</div>',

The following link shows you a number of ways you can use href some are good and others are bad. 以下链接向您展示了许多可以使用href的方法,其中有些是好的,有些是不好的。 Have a look at https://stackoverflow.com/a/11348403/886393 看看https://stackoverflow.com/a/11348403/886393

specifically the answer by username demp. 具体来说就是用户名demp的答案。

Thanks, Paras 谢谢,帕拉斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM