js或者jquery直接下载网页上的图片代码

1、jquery方式

使用jquery直接下载图片

function
downloadImage(src) { var a = $("<a></a>").attr("href", src).attr("download", "img.png").appendTo("body"); a[0].click(); a.remove(); }

 2,兼容模式IE下

IE下使用canvas 然后通过然msSaveBlob来方法保存图片

function downloadImage(src) {
    var canvas = document.createElement('canvas');
    var img = document.createElement('img');
    img.onload = function(e) {
        canvas.width = img.width;
        canvas.height = img.height;
        var context = canvas.getContext('2d');
        context.drawImage(img, 0, 0, img.width, img.height);
        window.navigator.msSaveBlob(canvas.msToBlob(),'image.jpg');
    }
    img.src = src;
}

 以上代码亲测可用

原文地址:https://www.cnblogs.com/bieanju/p/6944276.html