当只有lastChild节点应该响应点击时,是什么导致我的图像节点响应点击
What is causing my image nodes to be responsive to clicks when only the lastChild node is supposed to?
我正在开发一个迷你javascript来寻找差异游戏。从本质上讲,每个div中都有两个div和微笑的图像,并且在左边的div上总是会有一个额外的微笑。你必须点击左边的那个额外的div。如果点击其他任何地方或任何其他人脸,它应该提醒用户并结束游戏。
因此,我查看了w3schools和Stackoverflow,但还没有发现任何有帮助的东西。
我遇到的问题是我的代码正在崩溃。而不是只在左div的lastChild上检测.onclick
。它在所有人脸上检测,因此每次添加5个微笑。
我在这里做错了什么?我查看了chrome调试工具,也找不到任何东西。
此外,当您单击leftSidediv上的其他任何位置时,它应该会提醒用户并结束游戏。在发出警报后,它并没有结束游戏,对进一步的点击没有反应,而是仍然对点击和添加微笑做出反应。
感谢所有的帮助。
附言:这是我关于堆栈溢出的第二个问题。如果我的问题格式不对,我会提前道歉。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Matching Game</title>
<style>
img {position:absolute;}
#leftSide {
position:absolute;
width:500px;
height:500px;
}
#rightSide {
width: 500px;
height: 500px;
position:absolute;
left: 500px;
border-left: 1px solid black;
}
</style>
</head>
<body onload="generateFaces()">
<h1>Matching Game</h1>
<p>
Find and click on the extra smiling face on the left.
</p>
<div id="leftSide">
</div>
<div id="rightSide">
</div>
<script type="text/javascript">
var numberOfFaces = 5;
var theLeftSide = document.getElementById("leftSide");
var theRightSide = document.getElementById("rightSide");
var theBody = document.getElementsByTagName("body")[0];
function generateFaces() {
for(var i=0; i<numberOfFaces; i++) {
var img = document.createElement("img");
img.src = "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png";
var y_position = Math.random() * 400;
y_position = Math.floor(y_position);
img.style.top = y_position + "px";
var x_position = Math.random() * 400;
x_position = Math.floor(x_position);
img.style.left = x_position + "px";
theLeftSide.appendChild(img);
var leftSideImages = theLeftSide.cloneNode(true);
leftSideImages.removeChild(leftSideImages.lastChild);
theRightSide.appendChild(leftSideImages);
theLeftSide.lastChild.onclick=
function nextLevel(event) {
while(theLeftSide.firstChild || theRightSide.firstChild) {
theLeftSide.removeChild(theLeftSide.firstChild);
theRightSide.removeChild(theRightSide.firstChild);
};
event.stopPropagation();
numberOfFaces += 5;
generateFaces();
};
theBody.onclick = function gameOver() {
alert("Game Over!");
theBody.onclick = null;
theLeftSide.lastChild.onclick = null;
};
}
}
</script>
</body>
</html>
您正在将获胜的onclick事件分配给左侧的每个项目。如果你只想把它添加到最后一个项目(不会被复制的项目),你需要告诉for循环so:
if (i === numberOfFaces - 1) {
theLeftSide.lastChild.onclick=
function nextLevel(event) {
while(theLeftSide.firstChild || theRightSide.firstChild) {
theLeftSide.removeChild(theLeftSide.firstChild);
theRightSide.removeChild(theRightSide.firstChild);
};
event.stopPropagation();
numberOfFaces += 5;
generateFaces();
};
}
这是小提琴(我把alert
改成了console.log
,打开控制台查看输出):http://jsfiddle.net/zhv38xrp/1/
相关文章:
- 节点,express应用程序返回text/javascript响应头(Content-Type),尽管设置了applic
- 如何发送作为节点请求响应函数中的代码块的响应
- 节点.js和表达式:如何在异步操作后返回响应
- 节点 js 中的请求和响应事件如何彼此不同
- 如何在节点中解码 gzip 或 utf-8 响应
- 回调返回响应,但当在API中发送时,响应在节点JS中显示为空白
- 如何读取在节点中发出的简单http请求的响应
- 在JavaScript节点环境中将Gzip内容编码响应转换为JSON数据
- 响应字段包含一个子级时的节点 SOAP 强制数组
- 无法将节点邮件程序错误响应从节点.js发送到角度.js
- 节点.js请求对象 - 返回响应正文以供进一步操作
- 节点.js教程 Web 服务器没有响应
- 节点服务器响应错误: process.nextTick(function(){throw err;});.
- 节点.js HTTP 响应流
- 节点.js浏览器响应:连接超时
- 如何从角度读取节点服务器的 json 响应
- 节点 + 角度:响应标头未在请求中传递
- 如何在发送响应节点js(Sails)之前设置cookie
- 在已经使用的响应节点js之后重定向
- JSON响应节点的JS错误