使用点击香草JS,在错误的节点上激活
Using on click vanilla JS, activating on wrong node
我几乎完成了我正在学习Dom操作的这类游戏。基本上,游戏在左边产生5个图像,在右边产生4个图像,你点击奇数,然后在左边产生10个图像,右边产生9个图像(每次+5)。
我希望我的下一级函数在每次单击(左侧)的最后一个子节点时都能工作。它第一次工作,但在那之后,无论我是否单击了正确的节点,我的gameOver函数都会被调用,我不知道为什么。我试着删除了游戏over功能,但第二次我想让我的nextLevel运行(点击后),它没有。我是不是完全错了?感谢您的任何意见。留下了我的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>
您只需要在while
之后将onclick
移动到makeFaces
内部,因此每次创建它们后都会添加:-
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>
相关文章:
- 节点是否需要模块传递带有方括号的arg?这是个错误吗
- 节点分叉子进程并获取它's错误
- 是否排除节点中错误堆栈的第一行?/节点中的自定义错误类型
- Sinon Mocha节点错误处理
- 将节点.js更新为 0.12 标头错误
- 节点发布错误对象的第一个“属性”
- 尝试读取空节点时出现 XML JavaScript 错误
- 节点.js未捕获的异常类型错误:无法设置未定义的预处理 ''
- 节点.js 使用可写流写入文件时出现 EBADF 错误
- 解决节点中的“本地主机意外关闭连接”错误
- 创建节点时出现 Javascript DOM 错误
- 收到错误:无法在“节点”上执行“appendChild”:参数 1 的类型不是“节点”
- Bookshelf注册表插件和节点循环依赖性错误
- 在javascript中更改变量值时未发现节点错误
- Meteor:排队任务中出现异常:错误:无法执行'removeChild'在'节点':要
- 未捕获的类型错误:对象#<节点列表>没有方法'addEventListener'
- appendChild上的错误-试图在不存在节点的上下文中引用该节点
- JWT 未解码“JWT 格式错误” - 节点角度
- 解决 dojo 中的“类型错误:节点为空”错误
- 科尔多瓦生成错误:节点值未定义