在循环内的appendChild问题

问题描述 投票:0回答:1

我使用以下代码创建一个带有弹出功能的图像滑块,以显示缩放版本。这里为360度视图图像添加了叠加图像。

$('.imageSet').each(function(){
    $(this).css({left : left+'%'});
    var mainImgWrapper = document.createElement("DIV");
    var mainImg = document.createElement("IMG");
    mainImg.src = $(this).attr('data-big');
    mainImgWrapper.id = $(this).attr('data-img-id');
    var orbitImg = document.createElement("IMG");
    orbitImg.src = "/base/media/img/icons/rotate.png";
    orbitImg.className = 'orbitIcon';
    if($(this).hasClass('orbitView')){
        mainImgWrapper.className = 'mainImage orbitView clickable';
        mainImgWrapper.appendChild(orbitImg);
    }else{
        mainImgWrapper.className = 'mainImage clickable';
    }
    mainImgWrapper.style.left = 'calc('+(imgCount*100)+'% + '+imgCount+'px)';
    mainImgWrapper.appendChild(mainImg);
    $('#mainImages').append(mainImgWrapper);

    var popImgWrapper = document.createElement("DIV");
    var popImg = document.createElement("IMG");
    popImg.src = $(this).attr('data-big');
    popImgWrapper.dataset.imgId = $(this).attr('data-img-id');
    if($(this).hasClass('orbitView')){
        popImgWrapper.className = 'popImage orbitView';
        popImgWrapper.appendChild(orbitImg);
    }else{
        popImgWrapper.className = 'popImage';
    }
    popImgWrapper.style.left = (imgCount*100)+'%';
    popImgWrapper.appendChild(popImg);
    $('#imagePopupView').append(popImgWrapper);
    imgCount++;
    left = left + 25.5;
});

当.imageSet具有类“orbitView”时,我将orbitImg附加到mainImgWrapper并对popImgWrapper执行相同的操作。但是orbitImg只会附加到popImgWrapper。我在代码中或在执行环境中找不到任何问题。我可能会遗漏一些我自己无法看到的东西。

jquery html dom appendchild
1个回答
0
投票

我在这里找到了这个问题。至于HTML标准,你不能两次附加一个html节点。如果这样做,它将只执行最后一次尝试。原因是如果您不小心向html节点添加了ID,并且如果HTML允许您多次将该节点添加为子节点,则它将多次重复相同的ID。您可以通过以下两个小提琴在示例中看到它。它如何覆盖尝试将同一节点作为子节点附加,以及在追加不同节点时它是如何工作的。因此,如果您需要在循环中追加某些内容,则需要每次都创建一个新节点。在我的情况下,我甚至不得不在循环内两次追加相同的节点。所以每次循环时我都会创建两个新节点。

附加两次的相同子项仅适用于第二个div

var mainImgWrapper = document.createElement("DIV");
mainImgWrapper.style.width = 100+'px';
mainImgWrapper.style.height = 100+'px';
mainImgWrapper.style.background = 'red';

var secondImgWrapper = document.createElement("DIV");
secondImgWrapper.style.width = 100+'px';
secondImgWrapper.style.height = 100+'px';
secondImgWrapper.style.background = 'green';

var appendChild = document.createElement("IMG");
appendChild.src = 'https://images.pexels.com/photos/457702/pexels-photo-457702.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260';
appendChild.width = 100;

mainImgWrapper.appendChild(appendChild);
secondImgWrapper.appendChild(appendChild);

document.body.appendChild(mainImgWrapper);
document.body.appendChild(secondImgWrapper);

附加的不同子项适用于两个div

var firstImgWrapper = document.createElement("DIV");
firstImgWrapper.style.width = 100+'px';
firstImgWrapper.style.height = 100+'px';
firstImgWrapper.style.background = 'red';

var secondImgWrapper = document.createElement("DIV");
secondImgWrapper.style.width = 100+'px';
secondImgWrapper.style.height = 100+'px';
secondImgWrapper.style.background = 'green';

var firstChildImg = document.createElement("IMG");
firstChildImg.src = 'https://images.pexels.com/photos/457702/pexels-photo-457702.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260';
firstChildImg.width = 100;

var secondChildImg = document.createElement("IMG");
secondChildImg.src = 'https://images.pexels.com/photos/457702/pexels-photo-457702.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260';
secondChildImg.width = 100;

firstImgWrapper.appendChild(firstChildImg);
secondImgWrapper.appendChild(secondChildImg);

document.body.appendChild(firstImgWrapper);
document.body.appendChild(secondImgWrapper);
© www.soinside.com 2019 - 2024. All rights reserved.