gpt4 book ai didi

javascript - 使用 on click vanilla JS,在错误的节点上激活

转载 作者:行者123 更新时间:2023-11-28 06:40:43 24 4
gpt4 key购买 nike

差不多完成了我正在努力学习 Dom 操作的这类游戏。基本上游戏会在左侧生成 5 个图像,在右侧生成 4 个图像,您单击奇数个图像,然后在左侧生成 10 个图像,在右侧生成 9 个图像(每次 +5)。

我希望每次单击(LeftSide 的)最后一个子项时我的下一级函数都能工作。它第一次工作,但之后无论我是否单击正确的节点,我的 gameOver 函数都会被调用,我不知道为什么。我尝试删除游戏结束功能,但第二次我希望我的下一个关卡运行(单击后),但它没有。我是否以完全错误的方式处理这个问题?如有任何意见,我们将不胜感激,谢谢。留下我的 gameOver 函数,这样你就可以看到我想用它做什么。

var theLeftSide = document.getElementById("leftside");
var theRightSide = document.getElementById("rightside");
var facesNeeded = 5;
var totalfFaces = 0;
var theBody = document.getElementsByTagName("body")[0];

function makeFaces() {
while (facesNeeded != totalfFaces) {
smiley = document.createElement("img");
smiley.src = "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png";
smiley.style.top = Math.random() * 401 + "px";
smiley.style.left = Math.random() * 401 + "px";
document.getElementById("leftside").appendChild(smiley);
totalfFaces++;
// alert(totalfFaces); used to debug
}
if (facesNeeded == totalfFaces) {
//alert(facesNeeded);
//alert(totalfFaces);
leftSideImages = theLeftSide.cloneNode(true);
leftSideImages.removeChild(leftSideImages.lastChild);
document.getElementById("rightside").appendChild(leftSideImages);
//alert("hi");
}
}

makeFaces();

function delFaces(side) {
while (side.firstChild) {
side.removeChild(side.firstChild);
}

}

theLeftSide.lastChild.onclick = function nextLevel(event) {
event.stopPropagation();
delFaces(theRightSide);
delFaces(theLeftSide);
totalfFaces = 0;
facesNeeded += 5;
//alert(facesNeeded);
//alert(totalfFaces);
makeFaces();

};

theBody.onclick = function gameOver() {
alert("Game Over!");
theBody.onclick = null;
theLeftSide.lastChild.onclick = null;

};
<!DOCTYPE html>
<html>

<head>
<style>
img {
position: absolute;
}
div {
position: absolute;
width: 500px;
height: 500px;
}
#rightside {
left: 500px;
border-left: 1px solid black;
}
</style>

</head>

<body>
<h1> Matching Game</h1>
<p>Click on the extra smiling face on the left</p>
<div id="leftside"></div>
<div id="rightside"></div>
<script src="script3.js"></script>
</body>

</html>

最佳答案

您只需将 makeFaces 中的 onclick 移到 while 之后,这样每次创建它们后都会添加它:-

var theLeftSide = document.getElementById("leftside");
var theRightSide = document.getElementById("rightside");
var facesNeeded = 5;
var totalfFaces = 0;
var theBody = document.getElementsByTagName("body")[0];

function makeFaces() {
while (facesNeeded != totalfFaces) {
smiley = document.createElement("img");
smiley.src = "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png";
smiley.style.top = Math.random() * 401 + "px";
smiley.style.left = Math.random() * 401 + "px";
document.getElementById("leftside").appendChild(smiley);
totalfFaces++;
// alert(totalfFaces); used to debug
}
if (facesNeeded == totalfFaces) {
//alert(facesNeeded);
//alert(totalfFaces);
leftSideImages = theLeftSide.cloneNode(true);
leftSideImages.removeChild(leftSideImages.lastChild);
document.getElementById("rightside").appendChild(leftSideImages);
//alert("hi");
}
theLeftSide.lastChild.onclick = function nextLevel(event) {
event.stopPropagation();
delFaces(theRightSide);
delFaces(theLeftSide);
totalfFaces = 0;
facesNeeded += 5;
//alert(facesNeeded);
//alert(totalfFaces);
makeFaces();

};
}

makeFaces();

function delFaces(side) {
while (side.firstChild) {
side.removeChild(side.firstChild);
}

}

theBody.onclick = function gameOver() {
alert("Game Over!");
theBody.onclick = null;
theLeftSide.lastChild.onclick = null;

};
<!DOCTYPE html>
<html>

<head>
<style>
img {
position: absolute;
}
div {
position: absolute;
width: 500px;
height: 500px;
}
#rightside {
left: 500px;
border-left: 1px solid black;
}
</style>

</head>

<body>
<h1> Matching Game</h1>
<p>Click on the extra smiling face on the left</p>
<div id="leftside"></div>
<div id="rightside"></div>
<script src="script3.js"></script>
</body>

</html>

关于javascript - 使用 on click vanilla JS,在错误的节点上激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33850281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com