如何獲得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文件底部的函數 –