如何在javascript中从数组创建html表格元素tilemap?

巴迪

如何从数组创建html表tilemap?例如,当我希望“ 0”是绿色背景,宽度和高度为20px的“ td”,而“ 1”是具有相同背景大小的棕色背景的“ td”时?谁能给这个数组一个例子?另外,我想知道如何将图片插入特定的“ td”元素?例如,位置表[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]
]
易卜拉欣·马尔里尔

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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Javascript 中创建 2darray tilemap 的边界?

如何在HTML表格中隐藏元素

如何在javascript中从HTML表创建对象数组?

如何在javascript中创建表格以及其中的所有元素和信息

如何在javascript中循环表格HTML?

如何在 Javascript 中插入 HTML 表格?

如何将包含 HTML 元素的数组转换为 Javascript 中的表格?

创建在表格 html/javascript 中显示/隐藏元素的按钮

如何在 html 中创建新元素?

如何在 JavaScript 中创建嵌套元素?

如何在 Google 表格中创建占位符数组?

JavaScript 如何在每个创建的元素中创建元素?

如何在javascript数组中存储元素?

如何在JavaScript中创建对象数组?

如何在Javascript中创建数组对象

如何在JavaScript中创建静态数组

如何在javascript中创建对象数组

如何在javascript中创建对象数组?

如何在javascript变量中找到模式并在数组中返回创建的元素?

如何在HTML / CSS中创建枚举的表格环境?

如何在Javascript中附加HTML元素?

如何在Javascript中获取`html`元素?

使用 Javascript 数组创建 HTML 表格?

如何从对象数组创建 HTML 表格?

如何在JavaScript中的特定HTML标签下创建元素

如何在HTML上的javascript中从Flask打印数组元素

如何在Javascript中从表格中删除元素..当元素在不同的<TD>中时

如何在JavaScript的HTML表格主体中插入行

在JavaScript中,如何优雅地创建数组元素的差异列表?