JS三种标签下载文件(不跳转页面)

536 阅读1分钟

在JavaScript中,最多可以使用以下三种标签来下载文件但不跳转页面: 

 1、标签:可以通过设置download属性和href属性来下载文件。

<a href="http://example.com/file.zip" download="file.zip">下载文件</a>

 2、标签:可以通过设置src属性和style.display属性来下载文件。

<iframe src="http://example.com/file.zip" style="display:none;"></iframe>

 3、

var link = document.createElement('a');
link.href = 'http://example.com/file.zip';
link.download = 'file.zip';
document.body.appendChild(link);