我在点击下载图像时遇到问题,它打开图像的URL而不是下载。我也尝试过其他StackOverflow Answers,但没有什么能真正解决我的问题
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<a class="d1" href="https://cdn.shopify.com/s/files/1/1816/0091/files/Artboard_3_1.png?11554335258293208175" download="aa.jpg">
<img src="https://cdn.shopify.com/s/files/1/1816/0091/files/Artboard_3_1.png?11554335258293208175" width="104" height="142"> <span>Click to Download</span>
</a>
<script>
document.querySelector(".d1").setAttribute("download", "filename.jpg");
</script>
</body>
</html>
d1
,但在.getElementsByClassName()
你搜索dl
。filename.jpg
面前的开场白.getElementsByClassName()
在这里是错误的选择,因为它返回一个“实时”节点列表(仅在某些用例中有用并且会损害所有其他用户的性能)并且因为您对节点列表不感兴趣,所以您试图查找只是一个元素。请改用.querySelector()
。<!DOCTYPE html>
<html>
<head>
</head>
<body>
<a class="d1" href="https://cdn.shopify.com/s/files/1/1816/0091/files/Artboard_3_1.png?11554335258293208175" download="aa.jpg">
<img src="https://cdn.shopify.com/s/files/1/1816/0091/files/Artboard_3_1.png?11554335258293208175" width="104" height="142"> <span>Click to Download</span>
</a>
<script>
document.querySelector(".d1").setAttribute("download", "filename.jpg");
</script>
</body>
</html>