簡體   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