<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<a href="girl.jpg" download="beautifulGirl">
<img src="girl.jpg" alt="">
</a>
<!-- 点击图片实现下载 -->
<img id="girlImg" src="girl.jpg" alt="">
<button class="downloadBtn" type="button" onclick="downloadImg()">下载图片</button>
</body>
<script>
function downloadImg(){
var img = document.getElementById('girlImg'); // 获取要下载的图片
var url = img.src; // 获取图片地址
var a = document.createElement('a'); // 创建一个a节点插入的document
var event = new MouseEvent('click') // 模拟鼠标click点击事件
a.download = 'beautifulGirl' // 设置a节点的download属性值
a.href = url; // 将图片的src赋值给a节点的href
a.dispatchEvent(event) // 触发鼠标点击事件
}
</script>
</html>