js使用a标签下载文件方法

386 阅读1分钟

看看

let href="www.baidu.com"
let Element = document.createElement('a');
Element.setAttribute('href', href);
Element.setAttribute('target', '_self');
Element.setAttribute("style", "display:none");
Element.setAttribute('id', 'DownloadToolsElementA');
// 防止反复添加
if(document.getElementById('DownloadToolsElementA')) {	document.body.removeChild(document.getElementById('DownloadToolsElementA'));}document.body.appendChild(Element);
Element.click()

使用window.open兼容性问题比较多,而且会有新窗口,影响用户体验

如果有什么问题和疑问请在下方留言,文章为原创,转载请注明!