![](/img/trans.png)
[英]how to display same image multiple times using same image in javascript
[英]How to upload 1 image + convert to base64 + display the same image with the same ID multiple times on the same HTML page with javascript?
它運行良好,但只顯示圖像一次...謝謝您的幫助
<input type="file" id="file-upload" class="upload-file bg-highlight shadow-s rounded-s " accept="image/*"> <img id="image-data" src="images/empty.png" class="img-fluid"> <img id="image-data" src="images/empty.png" class="img-fluid"> <img id="image-data" src="images/empty.png" class="img-fluid"> <img id="image-data" src="images/empty.png" class="img-fluid">
這可以按預期工作。 選擇一個圖像文件並使用FileReader
讀取圖像數據。
document.getElementById('file-upload').addEventListener('change', e => { const file = e.target.files[0]; const reader = new FileReader(); reader.addEventListener("load", function () { document.querySelectorAll('#image-data').forEach(i => i.src = reader.result); }); if (file) { reader.readAsDataURL(file); } });
#image-data { width: 100px; height: 100px; }
<input type="file" id="file-upload" class="upload-file bg-highlight shadow-s rounded-s " accept="image/*"> <img id="image-data" src="images/empty.png" class="img-fluid"> <img id="image-data" src="images/empty.png" class="img-fluid"> <img id="image-data" src="images/empty.png" class="img-fluid"> <img id="image-data" src="images/empty.png" class="img-fluid">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.