如何获得currentQuestion在createQuestion() 每次点击按钮时,currentQuestion将更新* 1次的更新,但在createQuestion()函数,currentQuestion的价值仍然是0。覆盖全局变量和另一个函数返回的JavaScript
var currenQuestion = 0;
function nxtQuestion() {
var button = document.getElementById('nextQuestion');
document.getElementById('genralCulture').addEventListener('click', function (event) {
if (event.target == button) {
currenQuestion += 1;
//createQuestion();
event.preventDefault();
}
}, false);
}
function createQuestion() {
var question = document.getElementById('theQuestion');
if (currenQuestion < allQuestions.length) {
nxtQuestion();
question.innerHTML = allQuestions[currenQuestion].question;
} else {
question.innerHTML = "Your score is "
}
}
在别人的话,当我点击该按钮,问题是没有更新,但事件处理程序工作正常
你在哪里调用'createQuestion()'? –
在html文件中。但早期,我已经调用了js文件底部的函数 –