尝试移除“leftSideImages”的最后一个子元素,使右侧的图像少于左侧的图像。以下是我的代码。目标是将左侧的图像克隆到右侧减去一个。谢谢!试图移除子元素的最后一个子元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Matching Game</title>
<style>
div {
position: absolute;
width: 500px;
height: 500px
}
img {
position: absolute
}
#rightSide {
left: 500px;
border-left: 1px solid black
}
</style>
<script>
function generateFaces(){
var numberOfFaces = 5;
var theLeftSide = document.getElementById("leftSide");
var theRightSide = document.getElementById("rightSide");
for (var i = 0; i< numberOfFaces; i++){
var random_top = Math.floor(Math.random() * 400);
var random_left = Math.floor(Math.random() * 400);
var img = document.createElement("img");
img.src="smile.png";
img.style.top = random_top + "px";
img.style.left = random_left + "px";
theLeftSide.appendChild(img);
}
var leftSideImages = theLeftSide.cloneNode(true);
//this doesn't work leftSideImages.removeChild(theLeftSide.lastChild);
// it gets rid of everything.
// I'm trying to clone the left side onto the right with one less image
theRightSide.appendChild(leftSideImages);
}
</script>
</head>
<body>
<h3>Matching Game</h3>
<p>Click on the extra smiling face on the left.</p>
<div id="leftSide"></div>
<div id="rightSide"></div>
<script>generateFaces()</script>
</body>
</html>
'theLeftSide.lastChild'不是'leftSideImages'的孩子,它是'theLeftSide'的孩子。 – Barmar
当你克隆'theLeftSide'时,你也克隆了所有的孩子,因为你说'deep = true'。 – Barmar
只是为了澄清。我想保持左侧完好无损。我只想删除右侧的一个图像。最后一个。谢谢。 – martinbshp