简体   繁体   English

如何用户输入图像到div

[英]How to user-input image to div

I want to add the input file from html as a image inside a div however I don't want to use get element buy class name because I have multiple divs and for each one I want to add special image.我想将来自 html 的输入文件作为图像添加到 div 中,但是我不想使用 get element buy class name 因为我有多个 div,并且我想为每个 div 添加特殊图像。 if I use any selector the image will be added to each div any one can help.如果我使用任何选择器,图像将被添加到每个 div 中,任何人都可以提供帮助。 Thanks谢谢

I use this, this will change the image to all div having this class but I want to add a special img to each div我使用它,这会将图像更改为具有此 class 的所有 div,但我想为每个 div 添加一个特殊的 img

edit: iam creating a new div each time and i want for each div I created to add a special img编辑:我每次都创建一个新的 div,我想为我创建的每个 div 添加一个特殊的 img

here is the full code to create new div and to add pic这是创建新 div 和添加图片的完整代码

mainspan.onclick = () => { // mainspan is button used to create div mainspan.onclick = () => { // mainspan 是用来创建 div 的按钮

let div = document.createElement("div");让 div = document.createElement("div");
div.classList.add("main-dd"); div.classList.add("main-dd"); let button = document.createElement("button");让按钮 = document.createElement("按钮"); let img = document.createElement("img");让 img = document.createElement("img"); img.classList.add("currentimg"); img.classList.add("currentimg");
div.appendChild(img); div.appendChild(img); button.classList.add("btnaddinfo"); button.classList.add("btnaddinfo");
button.innerHTML = "Add item"; button.innerHTML = "添加项目"; button.addEventListener("click", () => { addinfo.style.display = "block"; // the menue to add the new picture }); button.addEventListener("click", () => { addinfo.style.display = "block"; // 添加新图片的菜单 }); div.appendChild(button); div.appendChild(按钮);

const uploadPictureButton = document.querySelector(".photo-upload"); const uploadPictureButton = document.querySelector(".photo-upload");

uploadPictureButton.addEventListener("change", function () { displayPicture(this); }); uploadPictureButton.addEventListener("更改", function () { displayPicture(this); });

function displayPicture(input) { if (input.files && input.files[0]) { var reader = new FileReader(); function displayPicture(input) { if (input.files && input.files[0]) { var reader = new FileReader();

 reader.onload = function (e) { document.querySelector(".currentimg").setAttribute("src", e.target.result); }; reader.readAsDataURL(input.files[0]); } } }; })
 document.querySelector(".photo-upload"); uploadPictureButton.addEventListener("change", function () { displayPicture(this); }); let a; function displayPicture(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { document.querySelector(".currentimg").setAttribute("src", e.target.result); }; reader.readAsDataURL(input.files[0]); } }

You probably want to use URL.createObjectURL您可能想使用URL.createObjectURL

 document.querySelector("input").addEventListener("change",event=>{ document.querySelector("img").src = URL.createObjectURL(event.target.files[0]); })
 body { display: grid; grid: 1fr 1fr / 1fr; } img { width:4em; height: 4em; }
 <input type="file" accept="image/png, image/jpeg"/> <img/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM