js點選按鈕下載圖片、word、pdf、excel等
阿新 • • 發佈:2018-12-11
html:
<button class="download" onclick=" download('url')">
<i class="layui-icon layui-icon-download-circle"></i>
</button>
url為要下載原始檔的地址。
Javascript
function download(src) { var $a = document.createElement('a'); $a.setAttribute("href", src); $a.setAttribute("download", ""); var evObj = document.createEvent('MouseEvents'); evObj.initMouseEvent( 'click', true, true, window, 0, 0, 0, 0, 0, false, false, true, false, 0, null); $a.dispatchEvent(evObj); }