简体   繁体   English

如何动态将img添加到div

[英]how to add img to div dynamically

there is an array with data有一个包含数据的数组

let mapImages = [
        {way: 'C:/Users/djoni/Desktop/Lessons/document/Homework_document/Images/1.jfif',
                         description: 'picher  1'},
        {way: 'C:/Users/djoni/Desktop/Lessons/document/Homework_document/Images/2.jfif',
                    description: 'picher 2'},......]

there are several divS有几个div

<div class="mini">   </div>


        <div id="myModal" class="modal">
            <div class="modal-content">
              <span class="close">&times;</span>
                   <img id ="pop" src ="" alt = ""  width="500" height="500" style="border: 2px solid red">
                    </div>
            </div>

i want my array pictures to be added to mini class div我希望将我的数组图片添加到迷你 class div

function showImages() {
            document.body.innerHTML = "";
            for(let i = 0; i < mapImages.length; i++){
                document.body.innerHTML +=  ` <img src="${mapImages[i].way}" alt = "${mapImages[i].description}"  width="200" height="200" style="border: 2px solid red"> `;
            }
        }

        showImages();

the problem is that pictures delete everything else on the page, and are not added to the desired div (mini class).问题是图片会删除页面上的所有其他内容,并且不会添加到所需的 div(迷你类)中。

Without use jquery or other frameworks不使用 jquery 或其他框架

You are clearing the content of the body inside the function, so remove that.您正在清除 function 内的正文内容,因此请将其删除。 Also, you should update the content of the element with class mini instead of body :此外,您应该使用 class mini而不是body更新元素的内容:

 let mapImages = [ {way:'C:/Users/djoni/Desktop/Lessons/document/Homework_document/Images/1.jfif',description: 'picher 1'}, {way:'C:/Users/djoni/Desktop/Lessons/document/Homework_document/Images/2.jfif',description: 'picher 2'} ]; var miniClass = document.querySelector('.mini'); function showImages() { for(let i = 0; i < mapImages.length; i++){ miniClass.innerHTML += ` <img src="${mapImages[i].way}" alt = "${mapImages[i].description}" width="200" height="200" style="border: 2px solid red"> `; } } showImages();
 <div class="mini"></div> <div id="myModal" class="modal"> <div class="modal-content"> <span class="close">&times;</span> <img id ="pop" src ="" alt = "" width="500" height="500" style="border: 2px solid red"> </div> </div>

Try this (get the div using getElementsByClassName and add/append all your image tags)试试这个(使用getElementsByClassName获取 div 并添加/附加所有图像标签)

function showImages() {
   let imgContainer = document.getElementsByClassName('mini')[0];

            for(let i = 0; i < mapImages.length; i++) {
               imgContainer.innerHTML +=  ` <img src="${mapImages[i].way}" alt = "${mapImages[i].description}"  width="200" height="200" style="border: 2px solid red"> `;
            }
}

use id not class使用 id 不是 class

<div id="mini">   </div>

and get it并得到它

document.getElementById('mini').innerHTML += ....

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

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