gpt4 book ai didi

Javascript FOR 循环回调

转载 作者:行者123 更新时间:2023-11-28 19:27:03 25 4
gpt4 key购买 nike

有一个项目 ID gameIds[i]_container 列表,显示在 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));
};

最佳答案

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

试试这个:

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

关于Javascript FOR 循环回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27661938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com