简体   繁体   English

Javascript-如何创建元素,将其添加到数组中并显示它

[英]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. 我想我感到困惑,但我无法真正理解在哪里或为什么。

  • I create an image with "let img = document.createElement('img');" 我使用“ let img = document.createElement('img');”创建图像
  • Then, I set its properties as object, and I think it is here where I get messed up. 然后,我将其属性设置为对象,我认为它是在这里弄乱的。 Then, I put this object into my array (called allImages), so, this is what I want so far. 然后,将这个对象放入数组(称为allImages)中,这就是我到目前为止想要的。
  • But then, a part from adding this object into my array, I want to display it as an HTML image, I am trying this with "appendChild", But definitely is something really wrong because I get the error that I am not operating with a node element. 但是,从将该对象添加到我的数组中之后,我想将其显示为HTML图像,我正在尝试使用“ appendChild”,但这绝对是错误的,因为我得到的错误是我没有使用节点元素。

Can someone give me a push to the right path? 有人可以推动我走上正确的道路吗?

Thank you 谢谢

Here I corrected your codepen . 在这里,我更正了您的codepen

You have to create a DOM element in order to add it to the page: 您必须创建一个DOM element才能将其添加到页面中:

var imgHTML = document.createElement("IMG");
imgHTML.setAttribute("id", img.id);
imgHTML.setAttribute("src", img.src);

Tested and working. 经过测试和工作。

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.

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