JS实现点击图片时将图片下载在本地2

凨影 1508

<img class="img1" src="https://bbs.oddfox.cn/view/img/logo.png" />
<a style="color:blue;" onclick="downloadIamge(".img1", "二维码.jpg")">下载二维码</a>
<script>
function downloadIamge(selector, name){
    // 通过选择器获取img元素,
    var img = document.querySelector(selector)
    // 将图片的src属性作为URL地址
    var url = img.src
    var a = document.createElement("a")
    var event = new MouseEvent("click")
    a.download = name || "下载图片名称"
    a.href = url
    a.dispatchEvent(event)
}
</script>
<script>
//页面实现简单的下载功能
function OpenQzqdUrl(obj){
    var file = obj.getAttribute("file");
    var sxUrl = "";
    if(file!=null){
        var url = sxUrl + file;
        window.open(url);
    }
}
</script>
<a href="javascript:void(0);" file="https://bbs.oddfox.cn/view/img/logo.png" onclick="OpenQzqdUrl(this)">下载图片</a>
联系QQ:77798085 赞助请点这里
全部回复 (1)