简体   繁体   English

<a>使用Jquery动态</a>添加“下载”属性

[英]Adding 'download' attribute to <a> dynamically using Jquery

I am trying to add the download attribute to an tag dynamically. 我正在尝试将download属性动态添加到标签。 Here is what I have so far: 这是我到目前为止的内容:

$(fclass).append('<a href="/files/'+$days[$i][2][1]+'" download>'+$days[$i][2][0]+'</a>');

Which produces this: 产生此结果:

<a href="/files/day0.pdf" download="">Slideshow (Notes)</a>

I also added the exact same tag as this produces into my HTML manually, but instead of 我也将与之完全相同的标记手动添加到我的HTML中,但不是

<a href="/files/day0.pdf" download="">

I used 我用了

<a href="/files/day0.pdf" download>

The manual version works perfectly, and automatically downloads the file, however, the dynamically placed version does not. 手动版本运行完美,可以自动下载文件,但是动态放置的版本则不能。 I have also tried this: 我也尝试过这个:

$(a).attr("download","/files/day0.pdf");

But this does not seem to work. 但这似乎不起作用。 Any ideas on how to fix this? 有想法该怎么解决这个吗? Thank you very much. 非常感谢你。

The answer is 答案是

 $("a").attr("download", true); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="file-to-download.txt">Download the file</a> 

Just checked an it is working :D 刚刚检查了它是否起作用:D

I had the same problem, this worked for me: 我有同样的问题,这对我有用:

$("a").replaceWith('<a href="/files/day0.pdf" download="day0.pdf">Slideshow (Notes)</a>');

Where the day0.pdf in the download="day0.pdf" will be the name of the file. 凡在该day0.pdf download="day0.pdf"将是该文件的名称。

(In Firefox you have to include the .pdf to the name, but in Chrome it is not necessary, it will include the extension with it.) (在Firefox中,您必须在名称中包含.pdf ,但在Chrome中则没有必要,它将包含扩展名。)

您尝试使用道具了吗?

 $(a).prop("download","/files/day0.pdf"); 

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

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