[英]Selecting the last element in a table
我正在創建一個小游戲,在其中創建了一個正方形表,並且第一個單擊左下角的塊會丟失。
我在程序中編輯左下角[point:(1,1)]正方形時遇到問題。 我的目標是在我的.CSS中有一個綠色的正方形,標記為#poison
#poison {
width: 5em;
height: 5em;
padding: 0;
background-size: 5em 5em;
background-image: url("http://imgur.com/6Apk8Rk");
}
出現,而不是我當前正在輸出的常規棕色正方形。
我不確定如何用#poison廣場代替左下角的廣場。
這是我用來創建表格的JS函數。
function makeTable(x, y) {
var gameTable = document.getElementById('gameTable'),
poison = document.getElementById('poison');
gameTable.innerHTML = null;
var table = [];
//creates rows
for (let i = 0; i < x; i += 1) {
var row = document.createElement('tr');
//appends each row to the table
gameTable.appendChild(row);
//store the amount of iterations in array trow
var tRow = [];
table[i] = tRow;
//create columns (cells)
for (let j = 0; j < y; j += 1) {
let cell = document.createElement('td'); //td defines a cell
//set x,y coordinates
cell.pos = { x: i, y: j };
//stores table values into cell
table[i][j] = cell;
//if you click the bottom left piece of chocolate..
if (i === x - 1 && j === 0) {
cell.onclick = function() {
//..restart the game
restartGame();
}
//else, behave normally
} else {
cell.onclick = function() {
cellClick(cell);
}
}
//append cells to the table
row.appendChild(cell);
}
}
}
以下是根據要求提供的相關HTML:
<fieldset id="RowColbox">
<label>
<span>Rows:</span>
<input name="Cols" id="cols" type="number" value="3"/>
</label>
<label>
<span>Columns:</span>
<input name="Rows" id="rows" type="number" value="4"/>
</label>
<button id="addDimensions" type="button">create</button>
</fieldset>
<table id="gameTable">
</table>
這是我的全部代碼。
假設您的代碼尚未完成,您可以通過以下方式為特定單元格分配id“毒葯”(例如: {x:1, y:1}
):
//set x,y coordinates
cell.pos = { x: i, y: j };
if(cell.pos.x === 1 && cell.pos.y === 1){
cell.id = "poison";
}
這是在左下欄添加“毒葯”的方法
if(cell.pos.x === (x-1) && cell.pos.y === 0){
cell.id = "poison";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.