【发布时间】:2021-07-08 19:03:20
【问题描述】:
我想将文档的所有图片 src 更改为 dataURL。
我正在尝试通过 for of 循环在画布中绘制所有图像,但它不起作用。 帮帮我!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<img src="./adudio1.png" alt=""height="300px"width="200px"class="i">
<img src="./adudio1.png" alt=""height="300px"width="500px"class="i">
</body>
<script>
const img = document.querySelectorAll("img");
for(items of img){
let c = document.createElement("canvas");
document.querySelector("body").append(c);
c.height=items.height;
c.width=items.width;
c.style="border:2px solid #CCC;";
ctx = c.getContext("2d");
ctx.drawImage(items,0,0)
}
</script>
</html>
【问题讨论】:
-
您能否详细说明“它不起作用?”
-
你为什么不直接改变
<img>的src属性呢?