最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - setAttribute download for anchor tag using JavaScript - Stack Overflow

programmeradmin5浏览0评论

I want to use Javascript to make an anchor tag inside a button, so that, when I click this button, it will download a specified file I set before. But I don't know how to add attribute "download" when using Javascript create it.

function myFunction() {
    var mydiv = document.getElementById("myDiv");
    var aTag = document.createElement('a');
    aTag.setAttribute('href',"abc/example.exe");
    aTag.innerHTML = "<button>GO</button>";
    mydiv.appendChild(aTag);
}

I want to use Javascript to make an anchor tag inside a button, so that, when I click this button, it will download a specified file I set before. But I don't know how to add attribute "download" when using Javascript create it.

function myFunction() {
    var mydiv = document.getElementById("myDiv");
    var aTag = document.createElement('a');
    aTag.setAttribute('href',"abc./example.exe");
    aTag.innerHTML = "<button>GO</button>";
    mydiv.appendChild(aTag);
}
Share Improve this question asked Feb 8, 2018 at 16:39 Yuki WatayuYuki Watayu 931 gold badge1 silver badge4 bronze badges 2
  • 1 aTag.setAttribute('download',"download"); – Scott Marcus Commented Feb 8, 2018 at 16:40
  • God =)) Thanks so much. – Yuki Watayu Commented Feb 8, 2018 at 16:42
Add a ment  | 

1 Answer 1

Reset to default 13

download is a Boolean attribute. That is, in HTML, no value is required to use it. The mere presence of the attribute is enough to make it work. Because of this, any value you might place on it isn't going to affect it working or not.

So, in situations like this, where you need to e up with a value for it, the remendation is to use the attribute name as the value, so your code would be:

aTag.setAttribute('download',"download");

Other examples of Boolean attributes are: disabled and readonly.

发布评论

评论列表(0)

  1. 暂无评论