javascript - 使用javascript更改 anchor 的下载属性的值

标签 javascript html download rename

我想更改 download 的值使用 javascript 属性,我正在尝试使用 download <a> 的属性标签。但我不知道如何在 download 中包含 javascript ?

<a href="REALFILENAME.txt" download="JAVASCRIPT_FUNCTION_HERE">CLICK ME</a>

function getDownloadName(){
    return "REALFILENAME_" + new Date().getTime();+ ".txt";
}

谢谢!

最佳答案

您可以通过使用 setAttribute 设置下载属性来完成此操作如下:

function getDownloadName(){
    return "REALFILENAME_" + new Date().getTime();+ ".txt";
}

function setDownloadAttribute() {
    var a = document.getElementById("fileLink");
    a.setAttribute("download", getDownloadName())
}

var a = document.getElementById("fileLink");
a.onclick = setDownloadAttribute;

jsFiddle

关于javascript - 使用javascript更改 anchor 的下载属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28428417/

相关文章:

android - 以编程方式下载视频android的第一帧

javascript - jQuery $.each 多个 div 名称

javascript - Protractor 自定义预期条件失败并出现错误

javascript - 我的 javascript 代码的语法错误

javascript - Javascript 中的警告框

html - 添加可见性 :Hidden effect and keep <a href> download ability

使用隐藏 IFrame 下载 PHP 文件而不保存

javascript - OAuth.io 连接失败

类的 Javascript 索引

javascript - Spectrum.js 在 angularjs 中使用时未初始化