【发布时间】: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));
};
【问题讨论】:
-
您的问题提到了
gameIds[i]_container,但这不是有效的Javascript。 -
我不认为这是重复的。他的
gameAccessCallback函数解决了这些重复项中的问题。他只是没有正确绑定事件。
标签: javascript loops callback