【问题标题】:jQuery cycle through element attributejQuery循环遍历元素属性
【发布时间】:2018-04-13 09:52:40
【问题描述】:

使用 jQuery,考虑到变量 dxl 是指向 .pdf 文件的链接数组,当单击按钮时,如何在元素的属性中循环 dxl 数组的值?

例子:

HTML

<a href="#"> Push </a>

jQuery

var dxl = ["d.pdf", "x.pdf", "l.pdf"];

$("a").click(function(){
    $("#button").attr("href", dxl);
    $("#button").attr("download", pdf_name);
});

点击Push按钮后希望下载3个.pdf文件;循环 href 属性中的数组值。单击按钮,href 接收第一个数组值,下载它,然后将下一个数组值传递给href(无需再次单击按钮)。

【问题讨论】:

  • 你可以为每个文件创建一个临时的a元素,如果你愿意,可以通过javascript点击它。

标签: javascript jquery html each


【解决方案1】:

希望这会奏效...

var dxl = ['d.pdf', 'x.pdf', 'l.pdf'];

for(var href of dxl){
  console.log(href);

  jQuery('#button').attr('href',href);
  jQuery('#button').trigger('click');
}

【讨论】:

    【解决方案2】:

    我认为最好的方法是在单独的新窗口中打开文件,因为http不支持下载多个文件。

    var dxl = ["d.pdf", "x.pdf", "l.pdf"];
    $("a").click(function(){
        for (var i = 0; i < dxl.length; i++){
            window.open(dxl[i]);
        }
    });
    

    【讨论】:

      【解决方案3】:

      你可以遍历一个数组:

      dxl.map(function(val){
         console.log(val); //will print d.pdf, x.pdf and l.pdf
      });
      

      【讨论】:

        猜你喜欢
        • 2013-04-11
        • 1970-01-01
        • 2010-09-15
        • 2010-10-28
        • 2018-06-05
        • 2013-07-22
        • 1970-01-01
        • 2011-04-13
        相关资源
        最近更新 更多