快速回答有关此代码无效的原因。我有一种感觉,它可能与变量范围有关,但我无法确定错误。我可以将div id打印到控制台,但是我得到一个错误,说明在语句中未定义id attr。
var game = {
currentPlayerTurn: players.firstPlayer.token,
displayToken: function() {
$('.unit').click(function() {
if(game.currentPlayerTurn === 'X' && !$(this).hasClass('selected')) {
$(this).addClass('selected').removeClass('unit').text("X");
game.currentPlayerTurn = players.secondPlayer.token;
} else if(game.currentPlayerTurn === 'O' && !$(this).hasClass('selected')) {
$(this).addClass('selected').removeClass('unit').text("O");
game.currentPlayerTurn = players.firstPlayer.token;
}
game.win($(this));
})
},
win: function(div) {
game.winCombos.forEach(function(element) {
element.forEach(function(smell){
if(smell === div.attr('id')) {
smell = game.currentPlayerTurn;
}
})})
console.log(div.attr('id'));
},
winCombos: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7], [2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7]]
};
game.win();
答案 0 :(得分:0)
game.win();
。你需要给它一些东西。
示例:
<div id="game"></div>
game.win($("#game"));