Javascript FOR 循环回调



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

我希望控制台在项目单击时记录该项目的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));
};

这里的问题是你正在做document.getElementById(...).click(). 穹顶没有click的方法! jQuery确实如此,但看起来你在这里并没有使用它。

试试这个:

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

最新更新