简体   繁体   English

如何将 append 多个 img-child 元素添加到一个带有 JS 的 DOM 元素中?

[英]how to append multiple img-child elements to a DOM element with JS?

I want to add multiple, same img-elements to a div element.我想向 div 元素添加多个相同的 img 元素。 Why can't I just use appendChild a couple times?为什么我不能只使用 appendChild 几次?

For instance:例如:

divelement.appendChild(imgelement);
divelement.appendChild(imgelement);
divelement.appendChild(imgelement);

will result in将导致

<div>
  <img></img>
</div>

But how can I achieve the following?但我怎样才能实现以下目标?

<div>
  <img></img>
  <img></img>
  <img></img>
</div>

The imgelement needs to be a new instance. imgelement需要是一个新实例。 If you add the same child, it will do nothing.如果你添加同一个孩子,它什么也不做。

 const divElement = document.querySelector('#target'); const imgElement = document.createElement('img'); divElement.append(imgElement); // Original reference divElement.append(imgElement); // ^ Same reference divElement.append(imgElement); // ^ Same reference
 img { width: 100px; height: 100px; background: red; }
 <div id="target"></div>

You need to create a new image instance for each append action:您需要为每个 append 操作创建一个新的图像实例:

 const divElement = document.querySelector('#target'); const createImgElement = () => document.createElement('img'); divElement.append(createImgElement()); divElement.append(createImgElement()); divElement.append(createImgElement());
 img { width: 100px; height: 100px; background: red; }
 <div id="target"></div>

This way should work.这种方式应该有效。

var div = document.getElementById('myId');
for(var i = 0; i < 3; i++){
      var img = document.createElement('img');
      div.appendChild(img);
}

I think You are appending same child 3 times.我认为您正在附加同一个孩子 3 次。

an working solution is here -一个可行的解决方案在这里 -

 const myParent = document.getElementById("someDiv"); const myImg1 = document.createElement("img"); myImg1.src = "https://picsum.photos/200/300"; myParent.append(myImg1); const myImg2 = document.createElement("img"); myImg2.src = "https://picsum.photos/200/300"; myParent.append(myImg2); const myImg3 = document.createElement("img"); myImg3.src = "https://picsum.photos/200/300"; myParent.append(myImg3);
 *{ margin:0; padding:0; box-sizing:border-box; } #someDiv{ width:500px; height:200px; background-color:green; margin:auto; display:flex; } img{ margin-right:10px; }
 <div id = "someDiv"> </div>

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

相关问题 如何将多个DOM元素附加到用reactjs创建的元素上? - How to append multiple DOM elements to an element created with reactjs? 是否可以将带有子元素的 html 元素保存为 JS 变量,您可以动态地将 append 保存到 DOM 中? - Is it possible to save an html element with child elements as JS variable which you can dynamically append to the DOM? 如何使用 vanilla javascript 在 DOM 中附加多个子元素并显示它们? - How to append multiple child elements in the DOM and display them, using vanilla javascript? 如何高效地创建和追加多个 dom 元素 - How to create and append multiple dom elements efficiently 使用vanilla javascript将单个子元素插入/附加到多个父元素 - Insert/Append single child element to multiple parent elements with vanilla javascript 将子元素附加到同一父元素中的子元素 - Append Child Elements to a Child Element in the same parent 如何创建一个元素并在反应中使用它对 append 子元素的引用? - How to create an element and use its ref to append child elements in react? JS DOM追加子项不起作用 - JS DOM append child does not work 如何选择一个元素的多个子元素 - How to select multiple child elements of an element jquery:如何将 DOM 元素附加到包含其他 DOM 元素的变量中的选择器 - jquery: how to append DOM element to selector within variable containing other DOM elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM