繁体   English   中英

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

[英]Add an event handler to an array

我希望当用户单击“下一步”按钮时移至下一个问题。 这里的问题是当我单击按钮时,它仅显示第一个问题。 问题出在哪儿? 我知道'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);

尝试这个 :

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