如何使用JavaScript从数组创建HTML表格元素瓦片图?

4
如何从数组创建HTML表格瓦片图?例如,当我想要将“0”作为具有绿色背景和20px宽度和高度的“td”,并且将“1”作为具有相同大小的棕色背景的“td”时,该怎么做?有人能给出这个数组的例子吗?此外,我想知道如何将图片插入到特定的“td”元素中?例如,在位置table [0] [0]上的“td”元素中插入一张树图片,并带有绿色背景,谢谢。
var table = [
[0,1,1,1,0],
[0,1,0,0,0],
[0,1,1,0,0],
[0,0,1,0,0],
[1,1,1,0,0]
]
1个回答

3

var arr = [                                        // the array
  [0, 1, 1, 1, 0],
  [0, 1, 0, 0, 0],
  [0, 1, 1, 2, 2],
  [0, 0, 1, 0, 2],
  [1, 1, 1, 0, 0]
];

var table = document.createElement("table");       // create a table element
var tbody = document.createElement("tbody");       // create a tbody element
table.appendChild(tbody);                          // add the tbody element to the table element

arr.forEach(function(sub, j) {                     // for each sub-array sub in the array arr (j is the index of this row)
  var row = document.createElement("tr");          // create a row element
  tbody.appendChild(row);                          // add the row element to the tbody element
  sub.forEach(function(num, i) {                   // for each number num in the array sub (i is the index of this column)
    var cell = document.createElement("td");       // create a cell element
    row.appendChild(cell);                         // add the cell element to this row element
    cell.className = num === 1? "brown": "green";  // if the number num is 1 then set the class to .brown, otherwise set it to .green
    cell.id = "cell-" + i + "-" + j;               // add an id to each cell so you can select it later
  });
});

// use table as you wish
document.body.appendChild(table);                  // in this example append it to the body

// ##################################################################################
// JUST AN EXAMPLE: (THE BELLOW CODE IS JUST TO GIVE YOU SOME INSIGHTS ON YOUR QUEST)
// ##################################################################################

var img = document.createElement("img");           // create the img element
img.src = "http://placehold.it/15";                // set its src

function moveTo(x, y) {                            // get a x and y and move the image to the cell at that pstion
  if(x < 0 || x > 4) return;                       // if x is out of bound, abort
  if(y < 0 || y > 4) return;                       // if y is out of bound, abort

  document.getElementById("cell-" + x + "-" + y).appendChild(img); // move the image img to cell-x-y (x from the left, and y from the top)
}

moveTo(2, 2);                                      // move to the third-third cell at start

document.addEventListener("click", function(e) {   // just an example: when clicking a td element, move the image to that cell
  var target = e.target;
  if(target.tagName === "TD")
    moveTo(target.id.match(/\d+/), target.id.match(/\d+$/));
});
.brown, .green {
  width: 20px;
  height: 20px;
}

.brown {
  background: brown;
}

.green {
  background: green;
}


谢谢,但您能否给我一个函数的例子,该函数遍历创建的表并在绿色背景前插入一棵树的图片?例如,当我有第二个数组trees = [ [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 0] ]时。 - bady
1
@bady 你可以使用一个数组来完成整个操作:如果数字是 1,那么颜色将会是 棕色,否则(任何其他数字)将会是 绿色。如果数字是 2,那么就在单元格中添加一张图片。请检查答案,我已经进行了编辑。 - ibrahim mahrir
谢谢,这几乎是我要找的,只是缺少一个小函数。我的意思是现在我已经在DOM表格中有了图片,我只需要一个能够在表格特定位置插入和删除图片的函数。所以,万一我已经创建了这张带有图片的表格,之后我想把一个图片从一个位置移动到另一个位置,那么我就需要从最后一个位置将它移除,并在新的位置插入它。再次感谢。 - bady
@bady 不用客气!请查看编辑过的内容。如果你想制作游戏,最好使用画布(canvas) - ibrahim mahrir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接