如何在上传图片时显示带有图片的评论字段



希望你们都做得很好。我的JavaScript不是很好,我有一个问题要问你。如何编写一个函数,为我们上传到网站的每一张图片显示一个评论字段。我想在上传图像时显示一个评论字段。这是我的代码,我只需要写一个函数或显示注释字段的东西。

<p>
<input
type="file"
accept="image/*"
name="image"
id="file"
onchange="loadFile(event)"
style="display: none"
multiple
/>
</p>
<button>
<label for="file" style="cursor: pointer">Upload to Album three</label>
</button>
var loadFile = function (event) {
const imagePreview = Array.from(event.target.files);
var output = document.getElementById('output');
imagePreview.forEach((image) => {
const img = document.createElement('img');
img.src = URL.createObjectURL(image);
img.height = 100;
output.appendChild(img);
});
};

您的函数loadFile运行良好,只是缺少了抛出null引用错误的输出。因此,您应该将div has-id输出添加到HTML 中

<div id="output"></div>

最新更新