[英]Function in my button wont reload another function
I want the "Continue Playing" button to reload the game but keep the score. 我希望“继续播放”按钮重新加载游戏,但保持得分。 After I click the button it keeps the score but doesn't give me the correct answer after I click the shape.
单击按钮后,它保持得分,但是单击形状后没有给我正确答案。 What am I missing here?
我在这里想念什么? Below is my code, you can also view it in jsFiddle to see it in action.
下面是我的代码,您也可以在jsFiddle中查看它,以查看其运行情况。
var shapeName = ["Square", "Rectangle"];
var score = 0;
var cssIdSquare = "<div id='square' onclick='sqrClick()'></div>";
var cssIdRec = "<div id='rect'onclick='rectClick()' ></div>";
var docMyAnswer = document.getElementById("myAnswer");
var docMyScore = document.getElementById("score");
var answerCorrect = "CORRECT!";
var answerWrong = "WRONG!";
// Random Shape to Select Title
var shapesTitle = shapeName[Math.floor(Math.random() * shapeName.length)];
document.getElementById("IdShapeTitle").innerHTML = shapesTitle.toString();
///// Function Shape Logic
function showAllShapes() {
///// Show both shapes
document.getElementById("showShapes").innerHTML = cssIdSquare + "<br>" + cssIdRec;
}
function sqrClick() {
if (shapesTitle === "Square") {
docMyAnswer.innerHTML = answerCorrect;
score += 1;
docMyScore.innerHTML = score;
}
if (shapesTitle === "Rectangle") {
docMyAnswer.innerHTML = answerWrong;
}
}
function rectClick() {
if (shapesTitle === "Rectangle") {
docMyAnswer.innerHTML = answerCorrect;
score += 1;
docMyScore.innerHTML = score;
}
if (shapesTitle === "Square") {
docMyAnswer = answerWrong;
}
}
showAllShapes();
// Continue Playing Button
function btnContPlay() {
var shapesTitle = shapeName[Math.floor(Math.random() * shapeName.length)];
document.getElementById("IdShapeTitle").innerHTML = shapesTitle.toString();
showAllShapes();
}
// Start Over Button
function btnStartOver() {
score = 0; // reset score to 0
document.getElementById("score").innerHTML = score;
location.reload();
}
http://jsfiddle.net/arevee/39rgqevy/ http://jsfiddle.net/arevee/39rgqevy/
*** Also if you have any tips on shortening the code, I would like to hear. ***另外,如果您有任何缩短代码的提示,我也想听听。 Also I would like to keep this using functions, my next project will be learning about objects.
另外,我想保留使用函数的功能,下一个项目将学习对象。
your problem is in shapeTitle variable
.Inside btnContPlay()
function you have a new shapeTitle
variable because you have started with it with var
keyword.Your sqarClick
and rectClick
functions don't have access to this newly created variable. 您的问题出在
shapeTitle variable
中。在shapeTitle variable
btnContPlay()
函数中,您有一个新的shapeTitle
变量,因为您以var
sqarClick
开始使用它。您的sqarClick
和rectClick
函数无法访问此新创建的变量。 Remember in javascript functions create their own scope .Your first shapeTitle
variable was on global scope . 请记住,在JavaScript函数创建自己的范围 。你的第一
shapeTitle
变量是在全球范围内 。
your btnContPlay()
function is: 您的
btnContPlay()
函数为:
function btnContPlay() {
var shapesTitle = shapeName[Math.floor(Math.random() * shapeName.length)];
document.getElementById("IdShapeTitle").innerHTML = shapesTitle.toString();
showAllShapes();
}
change it to 更改为
function btnContPlay() {
shapesTitle = shapeName[Math.floor(Math.random() *shapeName.length)]; //removed var from shapesTitle
document.getElementById("IdShapeTitle").innerHTML = shapesTitle.toString();
showAllShapes();
}
So without var
keyword ,it will not create a new variable inside function's scope,and now it will effect the global shapeTitle
variable.This is a scope related problem.Removing it will solve your problem. 因此,如果没有
var
关键字,它将不会在函数范围内创建新变量,现在它将影响全局shapeTitle
变量。这是与范围相关的问题。 shapeTitle
它可以解决您的问题。 jsfiddle 的jsfiddle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.