What is the best JavaScript code to create an img element

前端 未结 11 1999
攒了一身酷
攒了一身酷 2020-11-28 20:46

I want to create a simple bit of JS code that creates an image element in the background and doesn\'t display anything. The image element will call a tracking URL (such as

11条回答
  •  囚心锁ツ
    2020-11-28 21:14

    This is the method I follow to create a loop of img tags or a single tag as ur wish

    method1 :
        let pics=document.getElementById("pics-thumbs");
                let divholder=document.createDocumentFragment();
                for(let i=1;i<73;i++)
                {
                    let img=document.createElement("img");
                    img.class="img-responsive";
                    img.src=`images/fun${i}.jpg`;
                    divholder.appendChild(img);
                }
                pics.appendChild(divholder);
    

    or

    method2: 
    let pics = document.getElementById("pics-thumbs"),
      imgArr = [];
    for (let i = 1; i < 73; i++) {
    
      imgArr.push(``);
    }
    pics.innerHTML = imgArr.join('
    ')

提交回复
热议问题