简体   繁体   English

从2D Javascript数组创建动态表/网格

[英]Creating a dynamic table/grid from 2D Javascript array

I'm trying to create a 6x6 table to store data for a simple puzzle that I'm working on, but rather than display 6 rows of 6 cells, all I can seem to output is all 36 cells within a single table row. 我正在尝试创建一个6x6表格来存储我正在处理的一个简单难题的数据,但是我似乎只显示一个表格行中的所有36个单元,而不是显示6行的6个单元格。 Can anyone spot what I'm doing wrong here? 有人可以在这里发现我在做什么错吗? For context: B&W represent Blue and White, and the array holds the "solution" of the puzzle. 对于上下文:B&W表示蓝色和白色,并且数组包含难题的“解决方案”。

 //6x6 array var solutionArray = new Array(6); solutionArray[0] = new Array(6); solutionArray[1] = new Array(6); solutionArray[2] = new Array(6); solutionArray[3] = new Array(6); solutionArray[4] = new Array(6); solutionArray[5] = new Array(6); solutionArray[0][0] = "W"; solutionArray[0][1] = "W"; solutionArray[0][2] = "B"; solutionArray[0][3] = "B"; solutionArray[0][4] = "W"; solutionArray[0][5] = "B"; solutionArray[1][0] = "W"; solutionArray[1][1] = "B"; solutionArray[1][2] = "W"; solutionArray[1][3] = "B"; solutionArray[1][4] = "B"; solutionArray[1][5] = "W"; solutionArray[2][0] = "B"; solutionArray[2][1] = "W"; solutionArray[2][2] = "B"; solutionArray[2][3] = "W"; solutionArray[2][4] = "W"; solutionArray[2][5] = "B"; solutionArray[3][0] = "W"; solutionArray[3][1] = "B"; solutionArray[3][2] = "W"; solutionArray[3][3] = "W"; solutionArray[3][4] = "B"; solutionArray[3][5] = "B"; solutionArray[4][0] = "B"; solutionArray[4][1] = "B"; solutionArray[4][2] = "W"; solutionArray[4][3] = "B"; solutionArray[4][4] = "W"; solutionArray[4][5] = "W"; solutionArray[5][0] = "B"; solutionArray[5][1] = "W"; solutionArray[5][2] = "B"; solutionArray[5][3] = "W"; solutionArray[5][4] = "B"; solutionArray[5][5] = "W"; var x = document.createElement("TABLE"); x.setAttribute("id", "gridTable"); document.body.appendChild(x); for (i = 0; i < solutionArray[i].length; i++) { //output the row tag var y = document.createElement("TR"); y.setAttribute("id", "row"); document.getElementById("gridTable").appendChild(y) for (j = 0; j < solutionArray.length; j++) { ///output the td tag var z = document.createElement("TD"); var t = document.createTextNode(solutionArray[i][j]); z.appendChild(t); document.getElementById("row").appendChild(z); } } 

Notice you are using the same ID for every row. 注意,每行都使用相同的ID。 When you use document.getElementById this will always return the first node with this ID, because IDs uniquely identify a DOM node. 当您使用document.getElementById它将始终返回具有此ID的第一个节点,因为ID唯一地标识DOM节点。 It's not recommended to have more than one DOM node with the same ID. 不建议有多个具有相同ID的DOM节点。

Do this instead. 改为这样做。

var x = document.createElement("TABLE");
x.setAttribute("id", "gridTable");
document.body.appendChild(x);

for (i = 0; i < solutionArray[i].length; i++) {
    //output the row tag
    var y = document.createElement("TR");
    y.setAttribute("id", "row");
    document.getElementById("gridTable").appendChild(y);

    for (j = 0; j < solutionArray.length; j++) {

    ///output the td tag
    var z = document.createElement("TD");
    var t = document.createTextNode(solutionArray[i][j]);
    z.appendChild(t);
   // change document.getElementById("row") by the y variable.
    y.appendChild(z);
  }
}

You can set the reference returned by document.createElement to a variable and do whatever you want with it. 您可以将document.createElement返回的引用设置为变量,然后对其执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM