我希望当用户点击" next"按钮移动到下一个问题。这里的问题是当我点击按钮时,它只显示第一个问题。问题出在哪儿?我知道'返回'将结果返回给函数,但是下次按下按钮时,迭代器(i)不是i + 1?
var changeQuestion = function() {
for (var i = 0; i < allQuestions.length; i++) {
var newNode = document.createTextNode(allQuestions[i].question);
return question_paragraph.replaceChild(newNode, question_paragraph.firstChild);
}
};
EventUtil.addHandler(next_button, 'click', changeQuestion);
答案 0 :(得分:1)
试试这个:
var i = -1;
function changeQuestion() {
i = (i < allQuestions.length) ? (i+1) : -1;
var newNode = document.createTextNode(allQuestions[i].question);
return question_paragraph.replaceChild(newNode, question_paragraph.firstChild);
}
EventUtil.addHandler(next_button, 'click', changeQuestion);