2015-11-02 94 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

問題解決了... –