【问题标题】:Javascript FOR Loop CallbackJavascript FOR 循环回调
【发布时间】:2015-02-24 01:11:32
【问题描述】:

gameIds[i]_container 的项目 ID 列表显示在 FOR 循环中。开 这些正在显示,没有问题。

我希望控制台在项目点击时记录项目的gameIds[i]。我相信这应该通过回调来完成,但目前单击项目时没有任何反应。

请帮忙!

代码:

//CLICKING ON GAMES
//Active Game - bring user to game
//Newly Invited Game - ask user to accept invite, then either bring to game or delete from players list
//Rematch Game - recreate previous game

//Function
function gameAccess(i) {
    //REMATCH GAME
        if (currentRound[i] == roundWinners[i].length && currentRound[i] != 0 && currentRound[i] == numberOfRounds[i]){
            console.log("You clicked " + gameIds[i]);
        }
        //NEWLY INVITED GAME
        if (currentRound[i] == 0 && currentUserId != creator[i]) {
            console.log("You clicked " + gameIds[i]);
        }
        //ACTIVE GAME
        else{
            console.log("You clicked " + gameIds[i]);
        }
}

//Callback
function gameAccessCallback(i){
    return function(){
        gameAccess(i);
    };
}

//Iterate Through
for (i = 0; i < numberOf; i++) {
    document.getElementById(gameIds[i] + "_container ").click(gameAccessCallback(i));
};

【问题讨论】:

标签: javascript loops callback


【解决方案1】:

这里的问题是你在做document.getElementById(...).click()。 DOMElements 没有click 方法! jQuery 可以,但看起来你在这里没有使用它。

试试这个:

for (i = 0; i < numberOf; i++) {
    document.getElementById(gameIds[i] + "_container ").addEventListener('click', gameAccessCallback(i));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多