简体   繁体   English

如何克隆图像但删除最后一个孩子?

[英]How to Clone images but delete last child?

I want to clone images of the leftSide div to the rightSide div. 我想将leftSide div的图像克隆到rightSide div。 But I do have to delete the last child of the leftSide div element. 但是我确实必须删除leftSide div元素的最后一个孩子。 On page load my leftSide div should have 5 faces and the rightSide div should have 4 images. 在页面加载时,我的leftSide div应该有5张面孔,而rightSide div应该有4张图片。 How can I achieve this result? 我怎样才能达到这个结果? If I try to remove the lastChild from the rightSide div the faces disappears from the div. 如果我尝试从rightSide div中删除lastChild,则面孔将从div中消失。 I want to do this with javascrip. 我想用javascrip做到这一点。

Here's my code: 这是我的代码:

    function generateFaces(){
        var theLeftSide = document.getElementById("leftSide");  
        var width = 500, height = 500;
        var top_position = 0, left_position = 0,
        numberOfFaces = 5;
            for (var i = 0; i < 5; i++) {
            createElement(i);   
            numberOfFaces += 5;
        }
        var theRightSide = document.getElementById("rightSide");
        leftSideImages = theLeftSide.cloneNode(true);
        document.getElementById("rightSide").appendChild(leftSideImages);
        theRightSide.removeChild(theRightSide.lastChild);
        function createElement() {
            var image = document.createElement('img');
            image.src = "smile.png";
            image.style.position = 'absolute';
            image.style.top = top_position + "px";
            image.style.left = left_position + "px";
            theLeftSide.appendChild(image);
            top_position = Math.random() *   400 ;
            left_position = Math.random() *  400 ;  
        }
    };

Delete the last child of leftSideImages instead, that is where the images are. 而是删除leftSideImages的最后一个子leftSideImages ,即图像所在的位置。 theRightSide last child is leftSideImages which contains all the images. theRightSide最后一个孩子是leftSideImages ,其中包含所有图像。

leftSideImages.removeChild(leftSideImages.lastChild);

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

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