简体   繁体   English

将事件处理程序添加到数组

[英]Add an event handler to an array

I want when the user clicks the "next" button to move to the next question. 我希望当用户单击“下一步”按钮时移至下一个问题。 The problem here is when I click the button, it only show the first question. 这里的问题是当我单击按钮时,它仅显示第一个问题。 Where is the problem? 问题出在哪儿? I know 'return' returns the result to the function, but next time when I press the button, the iterator(i) is not i+1? 我知道'return'将结果返回给函数,但是下次按下按钮时,iterator(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);

Try this : 尝试这个 :

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM