[英]Javascript - How to create element, add it into array and display it
I am trying to create a web-app which allows the user to upload an image from their pc, and from that, it will create an object, and add it into an array (to have record of it and its properties) and also display it on screen. 我正在尝试创建一个网络应用程序,该应用程序允许用户从其PC上传图像,然后它将创建一个对象,并将其添加到数组中(以记录它及其属性)并显示它在屏幕上。
I have created a codepen: https://codepen.io/eve_mf/pen/XebVWv 我创建了一个Codepen: https ://codepen.io/eve_mf/pen/XebVWv
html: HTML:
<form id="form" action="" method="post" enctype="multipart/form-data">
<div class="file-uploader-wrapper">
<input id="file-uploader" class="file-uploader-input" type='file' onchange="retrieveImageFromUpload(this);"/>
<button class="file-uploader-btn" type="button">Select an image</button>
</div>
</form>
<div id="map-view"></div>
js: JS:
let allImages = [];
let displayImages = document.getElementById("map-view");
let imagesCounter = 0;
$(".file-uploader-btn").click(function() {
$("#file-uploader").click();
});
function retrieveImageFromUpload() {
let imgReader = new FileReader();
imgReader.readAsDataURL(document.getElementById("file-uploader").files[0]);
imgReader.onload = function (imgReaderEvent) {
let imageUploadedName = imgReaderEvent.target.result;
return readUploadedImage(imageUploadedName);
};
}
//create HTML image from the uploaded img
function readUploadedImage(imageUploadedName) {
imagesCounter++;
let img = document.createElement('img');
img = {
"id": 'screen-' + imagesCounter,
"src": imageUploadedName,
"alt": 'Default-alt'
};
allImages.push(img);
console.log(allImages);
var imgHTML = img.cloneNode(true);
imgHTML = "<img id='"+ img.id + "' src='" + img.src + "' alt ='" + img.alt + "' />";
displayImages.appendChild(imgHTML);
console.log("-------------");
}
My problem begins after creating the image element; 创建图像元素后,我的问题开始了; I think I am getting confused but I can not really understand where or why. 我想我感到困惑,但我无法真正理解在哪里或为什么。
Can someone give me a push to the right path? 有人可以推动我走上正确的道路吗?
Thank you 谢谢
You're redifining img
as a regular object literal, loosing the image in the process. 您正在将img
定义为常规对象文字,从而在处理过程中失去了图像。
You have to set the properties one by one 您必须一一设置属性
let img = document.createElement('img');
img.id = 'screen-' + imagesCounter;
img.src = imageUploadedName;
img.alt = 'Default-alt;
What you're doing is pretty much the same as 您正在做的事情与
let x = 'image'; x = {id : 4}; console.log(x); // no suprise, it's just {id : 4} and the string was lost
It was a node element: 这是一个节点元素:
let img = document.createElement('img');
But then you got rid of the node element and replaced it with a custom object: 但是随后您摆脱了node元素,并用自定义对象替换了它:
img = {
//...
};
This completely removed the node element, now all img
has is your custom object with those three properties. 这样就完全删除了node元素,现在所有 img
都是具有这三个属性的自定义对象。 Instead of creating a new object, just set the properties directly: 无需创建新对象,只需直接设置属性:
let img = document.createElement('img');
img.id = 'screen-' + imagesCounter;
img.src = imageUploadedName;
img.alt = 'Default-alt';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.