【发布时间】:2014-12-03 20:50:30
【问题描述】:
我正在尝试构建一个井字游戏板,它可以根据玩家的选择改变颜色。该部分有效,但是当尝试为玩家的潜在选择添加悬停预览功能时,我无法获得正确的结果。
本质上,玩家 1 的牌是黄色的,而玩家 2 的牌是蓝色的。现在,悬停功能导致所有点击的图块都是蓝色的,虽然它正确地悬停在空白图块上,但它已经反转了已经选择的图块的颜色。
知道发生了什么吗?
相关的jQuery:
count = 0
// hover classes for clear selection
$( "td" ).hover(function() {
if (count % 2 === 0) {
$(this).toggleClass('yellowHover');
return;
} else {
$(this).toggleClass('blueHover');
return;
};
});
// upon click change turns, countt++, change box value, check if winner
$('td').click(function() {
var row = $(this).parent().index();
var col = $(this).index();
if(player1Name=="" || player2Name==""){
alert("Please set player all the names.");
return;
}
// doesn't allow spot to be clicked on twice
if(board[row][col]!==0){
alert("This position is taken. Please try again.");
return;
};
// if count is even, player 1 is yellow
// if count is odd, player 2 is blue
if (count % 2 === 0) {
board[row][col] = 1;
$(this).addClass('yellow');
count++;
winnerCheck(1, player1Name);
draw();
messageBoard(player2Name + "'s turn. Click a circle to mark it blue.");
} else {
board[row][col] = 2;
$(this).addClass('blue');
count++;
winnerCheck(2, player2Name);
draw();
messageBoard(player1Name + "'s turn. Click a circle to mark it yellow.");
};
});
相关css:
.yellow {
background-color: #ffc300;
}
.blue {
background-color: #73d2c9;
}
.yellowHover {
background-color: #ffc300;
opacity: 0.5;
}
.blueHover {
background-color: #73d2c9;
opacity: 0.5;
}
相关html:
<table>
<tbody>
<tr class="box_row" >
<td id="box_0_0" class="box_cell" data-row="0" data-col="0" data-clicked="0"></td>
<td id="box_0_1" class="box_cell" data-row="0" data-col="1" data-clicked="0"></td>
<td id="box_0_2" class="box_cell" data-row="0" data-col="2" data-clicked="0"></td>
</tr>
<tr class="box_row">
<td id="box_1_0" class="box_cell" data-row="1" data-col="0" data-clicked="0"></td>
<td id="box_1_1" class="box_cell" data-row="1" data-col="1" data-clicked="0"></td>
<td id="box_1_2" class="box_cell" data-row="1" data-col="2" data-clicked="0"></td>
</tr>
<tr class="box_row">
<td id="box_2_0" class="box_cell" data-row="2" data-col="0" data-clicked="0"></td>
<td id="box_2_1" class="box_cell" data-row="2" data-col="1" data-clicked="0"></td>
<td id="box_2_2" class="box_cell" data-row="2" data-col="2" data-clicked="0"></td>
</tr>
</tbody>
</table>
【问题讨论】:
-
什么是计数变量?你的 HTML 在哪里?至少提供您使用的结构类型的描述,以便我们可以复制您的问题
-
@Larz 对此感到抱歉。刚刚添加了相关的html。 count 初始化为 0 并在每轮后加 1 以跟踪轮到谁。
标签: jquery html css hover tic-tac-toe