2014-04-16 86 views
0

如何获得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 " 
    } 
} 

在别人的话,当我点击该按钮,问题是没有更新,但事件处理程序工作正常

+0

你在哪里调用'createQuestion()'? –

+0

在html文件中。但早期,我已经调用了js文件底部的函数 –

回答

0

试试这个

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() { 
currenQuestion += 1; 
var question = document.getElementById('theQuestion'); 
if (currenQuestion < allQuestions.length) { 
    nxtQuestion(); 
    question.innerHTML = allQuestions[currenQuestion].question; 
} else { 
    question.innerHTML = "Your score is " 
} 
} 
+0

不工作,问题没有更新。 –

+0

请问您可以创建JSFiddle吗? –

+0

http://jsfiddle.net/e7gyC/ –