【发布时间】:2020-12-31 23:12:45
【问题描述】:
我制作了一个将图像作为输入的按钮,但我找不到显示图片的方法。 我试图创建一个空元素,然后像在其他网站上看到的那样更改其 URL,但它没有用。
-
HTML
<input id="myImage" class="photo-upload" type="file" accept="image/*, image/jpeg"> <img id="the-picture" width="200" /> -
JavaScript
const uploadPictureButton = document.querySelector(".photo-upload"); uploadPictureButton.addEventListener("click", displayPicture); function displayPicture(event) { let image = document.getElementById("myImage"); image.src = URL.createObjectURL(event.target.files[0]); };
【问题讨论】:
-
这已经被回答了。 Link here
-
这能回答你的问题吗? Preview an image before it is uploaded
标签: javascript image input display