2015-11-02 255 views
1

我正在尝试创建一个类似于战列舰或扫雷艇的小游戏。我试图创建如果使用if语句来点击函数,会根据存储在数组中某个位置的值更改div的颜色。但是,在编写代码之后,div总是变为与存储在数组中的某个东西相关联的颜色,当它变成不同的颜色时。我已经检查过,数组中的值是正确的,所以if语句似乎有些问题。我第一次尝试使用jQuery,所以它可能非常简单,我不知道。任何帮助表示赞赏!jquery函数与if语句无法正常工作

代码:

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
    <style> 
    .toggle { 
    width: 50px; 
    height: 50px; 
    background: #33ccff; 
    } 
    .toggle.grey{background-color: #D3D3D3; 
    transition: all 1s;} 
    .toggle.red{background-color: #FF0000; 
    transition: all 1s;} 
    </style> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
</head> 
<body> 


<table> 
<tr> 
<td><div class="toggle" id="1a"></div></td> 
<td><div class="toggle" id="1b"></div></td> 
<td><div class="toggle" id="1c"></div></td> 
<td><div class="toggle" id="1d"></div></td> 
<td><div class="toggle" id="1e"></div></td> 
<td><div class="toggle" id="1f"></div></td> 
<td><div class="toggle" id="1g"></div></td> 
<td><div class="toggle" id="1h"></div></td> 
<td><div class="toggle" id="1i"></div></td> 
<td><div class="toggle" id="1j"></div></td> 
</tr> 
<tr> 
<td><div class="toggle" id="2a"></div></td> 
<td><div class="toggle" id="2b"></div></td> 
<td><div class="toggle" id="2c"></div></td> 
<td><div class="toggle" id="2d"></div></td> 
<td><div class="toggle" id="2e"></div></td> 
<td><div class="toggle" id="2f"></div></td> 
<td><div class="toggle" id="2g"></div></td> 
<td><div class="toggle" id="2h"></div></td> 
<td><div class="toggle" id="2i"></div></td> 
<td><div class="toggle" id="2j"></div></td> 
</tr><tr> 
<td><div class="toggle" id="3a"></div></td> 
<td><div class="toggle" id="3b"></div></td> 
<td><div class="toggle" id="3c"></div></td> 
<td><div class="toggle" id="3d"></div></td> 
<td><div class="toggle" id="3e"></div></td> 
<td><div class="toggle" id="3f"></div></td> 
<td><div class="toggle" id="3g"></div></td> 
<td><div class="toggle" id="3h"></div></td> 
<td><div class="toggle" id="3i"></div></td> 
<td><div class="toggle" id="3j"></div></td> 
</tr><tr> 
<td><div class="toggle" id="4a"></div></td> 
<td><div class="toggle" id="4b"></div></td> 
<td><div class="toggle" id="4c"></div></td> 
<td><div class="toggle" id="4d"></div></td> 
<td><div class="toggle" id="4e"></div></td> 
<td><div class="toggle" id="4f"></div></td> 
<td><div class="toggle" id="4g"></div></td> 
<td><div class="toggle" id="4h"></div></td> 
<td><div class="toggle" id="4i"></div></td> 
<td><div class="toggle" id="4j"></div></td> 
</tr><tr> 
<td><div class="toggle" id="5a"></div></td> 
<td><div class="toggle" id="5b"></div></td> 
<td><div class="toggle" id="5c"></div></td> 
<td><div class="toggle" id="5d"></div></td> 
<td><div class="toggle" id="5e"></div></td> 
<td><div class="toggle" id="5f"></div></td> 
<td><div class="toggle" id="5g"></div></td> 
<td><div class="toggle" id="5h"></div></td> 
<td><div class="toggle" id="5i"></div></td> 
<td><div class="toggle" id="5j"></div></td> 
</tr><tr> 
<td><div class="toggle" id="6a"></div></td> 
<td><div class="toggle" id="6b"></div></td> 
<td><div class="toggle" id="6c"></div></td> 
<td><div class="toggle" id="6d"></div></td> 
<td><div class="toggle" id="6e"></div></td> 
<td><div class="toggle" id="6f"></div></td> 
<td><div class="toggle" id="6g"></div></td> 
<td><div class="toggle" id="6h"></div></td> 
<td><div class="toggle" id="6i"></div></td> 
<td><div class="toggle" id="6j"></div></td> 
</tr><tr> 
<td><div class="toggle" id="7a"></div></td> 
<td><div class="toggle" id="7b"></div></td> 
<td><div class="toggle" id="7c"></div></td> 
<td><div class="toggle" id="7d"></div></td> 
<td><div class="toggle" id="7e"></div></td> 
<td><div class="toggle" id="7f"></div></td> 
<td><div class="toggle" id="7g"></div></td> 
<td><div class="toggle" id="7h"></div></td> 
<td><div class="toggle" id="7i"></div></td> 
<td><div class="toggle" id="7j"></div></td> 
</tr><tr> 
<td><div class="toggle" id="8a"></div></td> 
<td><div class="toggle" id="8b"></div></td> 
<td><div class="toggle" id="8c"></div></td> 
<td><div class="toggle" id="8d"></div></td> 
<td><div class="toggle" id="8e"></div></td> 
<td><div class="toggle" id="8f"></div></td> 
<td><div class="toggle" id="8g"></div></td> 
<td><div class="toggle" id="8h"></div></td> 
<td><div class="toggle" id="8i"></div></td> 
<td><div class="toggle" id="8j"></div></td> 
</tr><tr> 
<td><div class="toggle" id="9a"></div></td> 
<td><div class="toggle" id="9b"></div></td> 
<td><div class="toggle" id="9c"></div></td> 
<td><div class="toggle" id="9d"></div></td> 
<td><div class="toggle" id="9e"></div></td> 
<td><div class="toggle" id="9f"></div></td> 
<td><div class="toggle" id="9g"></div></td> 
<td><div class="toggle" id="9h"></div></td> 
<td><div class="toggle" id="9i"></div></td> 
<td><div class="toggle" id="9j"></div></td> 
</tr><tr> 
<td><div class="toggle" id="10a"></div></td> 
<td><div class="toggle" id="10b"></div></td> 
<td><div class="toggle" id="10c"></div></td> 
<td><div class="toggle" id="10d"></div></td> 
<td><div class="toggle" id="10e"></div></td> 
<td><div class="toggle" id="10f"></div></td> 
<td><div class="toggle" id="10g"></div></td> 
<td><div class="toggle" id="10h"></div></td> 
<td><div class="toggle" id="10i"></div></td> 
<td><div class="toggle" id="10j"></div></td> 
</tr> 

<script> 
$(document).ready(function(){ 

var row1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
var row2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
var row3 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
var row4 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
var row5 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
var row6 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
var row7 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
var row8 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
var row9 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
var row10= [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 

var coords = [row1, row2, row3, row4, row5, row6, row7, row8, row9, row10]; 


//place 5 boats on grid 
placeBoat(); 
placeBoat(); 
placeBoat(); 
placeBoat(); 
placeBoat(); 

console.log(row1 + row2 + row3+ row4+ row5+row6+row7+row8+row9+row10); 
function placeBoat() { 
var row_num = getRow(); 
var column_num = getColumn(); 
console.log("row_num =" + row_num + " column_num = " +column_num); 
if (typeof coords[row_num][column_num + 1] == undefined) { 
    if ((coords[row_num][column_num] == 0) && (coords[row_num][column_num - 1] == 0)) { 
    coords[row_num][column_num] = 1; 
    coords[row_num][column_num - 1] = 1; 
     } 
    } 

else if ((coords[row_num][column_num] == 0) && (coords[row_num][column_num + 1] == 0)) { 
coords[row_num][column_num] = 1; 
coords[row_num][column_num + 1] = 1; 

    } 


else 
    placeBoat(); 
} 

//get a random number between 0 and 9 for row 
function getRow() { 
var row_num = Math.floor(Math.random()*(9)+0); 

return row_num; 
} 

//get a random number between 0 and 9 for column 
function getColumn() { 
var column_num = Math.floor(Math.random()*(9-0+1)+0); 

return column_num; 
} 

//Tiles will change color when clicked depending on values in array 

$("#1a").click(function() { 
if (coords[0][0]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 

}); 
$("#1b").click(function() { 
if (coords[0][1]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#1c").click(function() { 
if (coords[0][2]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#1d").click(function() { 
if (coords[0][3]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#1e").click(function() { 
if (coords[0][4]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#1f").click(function() { 
if (coords[0][5]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#1g").click(function() { 
if (coords[0][6]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#1h").click(function() { 
if (coords[0][7]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#1i").click(function() { 
if (coords[0][8]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#1j").click(function() { 
if (coords[0][9]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#2a").click(function() { 
if (coords[1][0]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#2b").click(function() { 
if (coords[1][1]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#2c").click(function() { 
if (coords[1][2]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#2d").click(function() { 
if (coords[1][3]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#2e").click(function() { 
if (coords[1][4]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#2f").click(function() { 
if (coords[1][5]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#2g").click(function() { 
if (coords[1][6]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#2h").click(function() { 
if (coords[1][7]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#2i").click(function() { 
if (coords[1][8]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#2j").click(function() { 
if (coords[1][9]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#3a").click(function() { 
if (coords[2][0]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#3b").click(function() { 
if (coords[2][1]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#3c").click(function() { 
if (coords[2][2]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#3d").click(function() { 
if (coords[2][3]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#3e").click(function() { 
if (coords[2][4]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#3f").click(function() { 
if (coords[2][5]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#3g").click(function() { 
if (coords[2][6]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#3h").click(function() { 
if (coords[2][7]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#3i").click(function() { 
if (coords[2][8]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#3j").click(function() { 
if (coords[2][9]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#4a").click(function() { 
if (coords[3][0]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#4b").click(function() { 
if (coords[3][1]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#4c").click(function() { 
if (coords[3][2]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#4d").click(function() { 
if (coords[3][3]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#4e").click(function() { 
if (coords[3][4]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#4f").click(function() { 
if (coords[3][5]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#4g").click(function() { 
if (coords[3][6]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#4h").click(function() { 
if (coords[3][7]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#4i").click(function() { 
if (coords[3][8]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#4j").click(function() { 
if (coords[3][9]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#5a").click(function() { 
if (coords[4][0]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#5b").click(function() { 
if (coords[4][1]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#5c").click(function() { 
if (coords[4][2]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#5d").click(function() { 
if (coords[4][3]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#5e").click(function() { 
if (coords[4][4]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#5f").click(function() { 
if (coords[4][5]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#5g").click(function() { 
if (coords[4][6]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#5h").click(function() { 
if (coords[4][7]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#5i").click(function() { 
if (coords[4][8]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#5j").click(function() { 
if (coords[4][9]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#6a").click(function() { 
if (coords[5][0]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#6b").click(function() { 
if (coords[5][1]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#6c").click(function() { 
if (coords[5][2]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#6d").click(function() { 
if (coords[5][3]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#6e").click(function() { 
if (coords[5][4]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#6f").click(function() { 
if (coords[5][5]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#6g").click(function() { 
if (coords[5][6]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#6h").click(function() { 
if (coords[5][7]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#6i").click(function() { 
if (coords[5][8]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#6j").click(function() { 
if (coords[5][9]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#7a").click(function() { 
if (coords[6][0]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#7b").click(function() { 
if (coords[6][1]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#7c").click(function() { 
if (coords[6][2]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#7d").click(function() { 
if (coords[6][3]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#7e").click(function() { 
if (coords[6][4]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#7f").click(function() { 
if (coords[6][5]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#7g").click(function() { 
if (coords[6][6]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#7h").click(function() { 
if (coords[6][7]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#7i").click(function() { 
if (coords[6][8]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#7j").click(function() { 
if (coords[6][9]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#8a").click(function() { 
if (coords[7][0]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#8b").click(function() { 
if (coords[7][1]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#8c").click(function() { 
if (coords[7][2]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#8d").click(function() { 
if (coords[7][3]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#8e").click(function() { 
if (coords[7][4]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#8f").click(function() { 
if (coords[7][5]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#8g").click(function() { 
if (coords[7][6]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#8h").click(function() { 
if (coords[7][7]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#8i").click(function() { 
if (coords[7][8]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#8j").click(function() { 
if (coords[7][9]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#9a").click(function() { 
if (coords[8][0]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#9b").click(function() { 
if (coords[8][1]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#9c").click(function() { 
if (coords[8][2]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#9d").click(function() { 
if (coords[8][3]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#9e").click(function() { 
if (coords[8][4]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#9f").click(function() { 
if (coords[8][5]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#9g").click(function() { 
if (coords[8][6]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#9h").click(function() { 
if (coords[8][7]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#9i").click(function() { 
if (coords[8][8]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#9j").click(function() { 
if (coords[8][9]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#10a").click(function() { 
if (coords[9][0]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#10b").click(function() { 
if (coords[9][1]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#10c").click(function() { 
if (coords[9][2]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#10d").click(function() { 
if (coords[9][3]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#10e").click(function() { 
if (coords[9][4]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#10f").click(function() { 
if (coords[9][5]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#10g").click(function() { 
if (coords[9][6]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#10h").click(function() { 
if (coords[9][7]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 
$("#10i").click(function() { 
if (coords[9][8]=0) 
    $(this).toggleClass("grey"); 
else 
    $(this).toggleClass("red"); 
}); 

console.log("lower right is: " + coords[9][9]); 
$("#10j").click(function() { 
if (coords[9][9]=0){ 
    $(this).toggleClass("grey");} 
else { 
    $(this).toggleClass("red");} 
}); 
}); 

</script> 

</body> 
</html> 
+0

只要改变你的“=”到“==”,它会触发完美 –

回答

1

记住toggleClass()不会清除已经存在的类,他们只是添加或删除参数。这里是JQuery文档的链接https://api.jquery.com/category/manipulation/

我建议查看删除类的过程,然后添加所需的类。

您在if条件中寻找的答案是您使用单个=(赋值运算符)而不是==(比较运算符)来检查您的运算符,并且您应该没问题。

+0

还有我傻,你用没注意到'='符号用于比较操作数的使用== –

+0

将您的评论更改为答案... –

+0

Dangit ....这是我最近第二次犯这个错误,感谢帮助和抱歉发布了一些明显的东西。 – Matt

0

您的解决方案如此复杂,我不知道从哪里开始。您将ID分配给所有事物并一直检查它们的方式是解决您的问题的一种非常复杂的方式。我建议使用多维数组来简化事情。这将使定位特定的表格单元更加容易,这样您就可以更专注于程序问题。现在的代码可能很难调试。看看这对多维数组的信息:

jQuery Create Multidimensional Array

+0

问题解决了... –