[英]How to Clone images but delete last child?
我想将leftSide div的图像克隆到rightSide div。 但是我确实必须删除leftSide div元素的最后一个孩子。 在页面加载时,我的leftSide div应该有5张面孔,而rightSide div应该有4张图片。 我怎样才能达到这个结果? 如果我尝试从rightSide div中删除lastChild,则面孔将从div中消失。 我想用javascrip做到这一点。
这是我的代码:
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 ;
}
};
而是删除leftSideImages
的最后一个子leftSideImages
,即图像所在的位置。 theRightSide
最后一个孩子是leftSideImages
,其中包含所有图像。
leftSideImages.removeChild(leftSideImages.lastChild);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.