js下载浏览器中的图片
jquery
function download(src) { var $a = $("<a></a>").attr("href", src).attr("download", "img.png"); $a[0].click(); }
原生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); };