图片下载

a标签下载图片

<a href="img/img_1.jpg" download="img_1">
     <img src="img/img_1.jpg" height="200px" width="300px">
</a>

 

button 点击下载图片(模拟a标签)

var btn = document.getElementsByTagName("button")[0];
    
    btn.onclick = function(){
        let imgUrl = document.getElementsByTagName("img")[0].src;//获取路径
        let aa = document.createElement("a");//创建a标签
        aa.download = "img_1";//设置a标签的download属性
        let event = new MouseEvent("click"); //创建事件
        aa.href = imgUrl; //设置a标签路径
        aa.dispatchEvent(event); //给a标签添加事件
    }
posted @ 2019-12-26 21:08  casf  阅读(100)  评论(0编辑  收藏  举报