为什么我的变量在javascript中返回未定义?

阿里·费希尔

我是javascript新手。我正在制作抽认卡程序。由于某些原因,尽管将变量lastPress定义为全局变量,但lastPress似乎返回undefined。但是,这不会导致JSHint错误。这是我的代码:

var i = 0;
var questions = ["1: Was the human migration caused by an ice age?", "True"];
var answer = [true, true];
var lastPress;
document.getElementById("question").innerHTML = questions[i];
function itsTrue() {
    lastPress = true;
}
function itsFalse() {
    lastPress = false;
}
function getAns() {
    if (answer[i] == lastPress) {
        document.getElementById("ans").innerHTML = "You're correct! The answer was " + lastPress;
    }
    else {
        document.getElementById("ans").innerHTML = "We're sorry, but that is not correct. You said " + lastPress + ". The answer was " + answer[i];
    }
    i++;
    document.getElementById("question").innerHTML = questions[i];
}
document.getElementById("true").onclick = function(){itsTrue()};
document.getElementById("true").onclick = function(){getAns()};
document.getElementById("false").onclick = function(){itsFalse()};
document.getElementById("false").onclick = function(){getAns()};
<!DOCTYPE HTML>
<html>
    <head>
        <title>Flash cards</title>
    </head>
    <body>
        <p id="question">This is where the question shows up</p>
        <p id="ans">This is where the result will show up</p>
        <button type="button" id="true">True</button>
        <button type="button" id="false">False</button>
    </body>
</html>

DCR

如果要为同一单击事件定义多个功能:同样,在第二次按下您的真假按钮后,i会增加到2,因此问题[2]未定义

var i = 0;
            var questions = ["1: Was the human migration caused by an ice age?", "True"];
            var answer = [true, true];
            var lastPress;
            document.getElementById("question").innerHTML = questions[i];
            function itsTrue() {
                console.log('true')
                lastPress = true;                
            }
            function itsFalse() {
                console.log('false');
                lastPress = false;                
            }
            function getAns() {
                if (answer[i] == lastPress) {
                    document.getElementById("ans").innerHTML = "You're correct! The answer was " + lastPress;
                }
                else {
                    document.getElementById("ans").innerHTML = "We're sorry, but that is not correct. You said " + lastPress + ". The answer was " + answer[i];
                }
                i++;
                console.log(i);
                document.getElementById("question").innerHTML = questions[i];
            }
            document.getElementById("true").onclick = function(){itsTrue();getAns();};
            //document.getElementById("true").onclick = function(){getAns()};
            document.getElementById("false").onclick = function(){itsFalse(),getAns();};
           //  document.getElementById("false").onclick = function(){getAns()};
<!DOCTYPE HTML>
<html>
    <head>
        <title>Flash cards</title>
    </head>
    <body>
        <p id="question">This is where the question shows up</p>
        <p id="ans">This is where the result will show up</p>
        <button type="button" id="true">True</button>
        <button type="button" id="false">False</button>
    </body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我的变量在带有array.forEach的嵌套函数中在Javascript中使用未定义

为什么我的对象返回未定义?

为什么我的JavaScript提升的局部变量返回未定义,而提升的全局变量返回空白?

为什么我的find方法返回未定义?

为什么我的诺言返回未定义?

JavaScript-为什么我的API调用变量返回未定义?

为什么我的get请求在我的图像文件树中返回未定义?

为什么我的函数总是返回未定义的?

为什么简写定义的变量在React中未定义

为什么在JavaScript中未定义“ [] [[]]”

为什么我的reducer返回未定义的React TypeScript

为什么我的递归函数返回未定义?

为什么我的变量表数据返回未定义?

为什么我的jquery变量在Firebug控制台中返回未定义的

为什么我的代码中已明确定义了未定义的变量,为什么呢?

为什么我在JavaScript中得到“未定义”

为什么我的警报在javascript中显示未定义

在 JavaScript 中,为什么这个全局变量在我的函数中未定义?

为什么我的电子邮件检查变量返回未定义的错误

为什么我的变量在我的函数之外未定义?

为什么在 javascript 中执行导入时,有时会返回未定义的变量?

为什么我的变量在 init 中未定义?

为什么我的 express-session 变量在不同的路线上返回“未定义”?

为什么我的事件变量在输入中未定义?

为什么我的 axios 帖子在我的功能组件中返回未定义?

为什么我的嵌套承诺然后返回未定义?

为什么我的函数返回“未定义”

为什么我在 useEffect 其他地方定义的变量在 react 中未定义?

为什么我的变量在 javascript 中未定义?