gpt4 book ai didi

javascript - 将二维数组输出到html文件中

转载 作者:行者123 更新时间:2023-11-29 16:45:12 25 4
gpt4 key购买 nike

我要为学校做一个项目,你可以制作一个内存游戏。网格为 6x6,排列在表格中。我想做的是,当用户单击“打印”按钮时,表格将填满二维数组中的数字。我已经创建了包含数字的数组,但无法将它们输出到 HTML 文件中的表格。

这是我的代码:

<!DOCTYPE html>

<head>
<link href = "matchinggame.css" rel = "stylesheet"/>


</head>

<body>
<table>
<tr>
<td id="cell00" ></td>
<td id="cell01" ></td>
<td id="cell02" ></td>
<td id="cell03" ></td>
<td id="cell04" ></td>
<td id="cell05" ></td>
</tr>
<tr>
<td id="cell10" ></td>
<td id="cell11" ></td>
<td id="cell12" ></td>
<td id="cell13" ></td>
<td id="cell14" ></td>
<td id="cell15" ></td>
</tr>
<tr>
<td id="cell20" ></td>
<td id="cell21" ></td>
<td id="cell22" ></td>
<td id="cell23" ></td>
<td id="cell24" ></td>
<td id="cell25" ></td>
</tr>
<tr>
<td id="cell30" ></td>
<td id="cell31" ></td>
<td id="cell32" ></td>
<td id="cell33" ></td>
<td id="cell34" ></td>
<td id="cell35" ></td>
</tr>
<tr>
<td id="cell40" ></td>
<td id="cell41" ></td>
<td id="cell42" ></td>
<td id="cell43" ></td>
<td id="cell44" ></td>
<td id="cell45" ></td>
</tr>
<tr>
<td id="cell50" ></td>
<td id="cell51" ></td>
<td id="cell52" ></td>
<td id="cell53" ></td>
<td id="cell54" ></td>
<td id="cell55" ></td>
</tr>
</table>

<div>
<input type="button" value="print" id = "button1" onclick="drawBoard();">
</div>


</body>

root {
display: block;
}

body {
text-align:center;
}

p {
font-family:monospace;
}

table {
margin:auto;
}

td {
width:50px;
height:50px;
border: 1px solid black;
font-size:40px;
text-align:center;
font-family:arial, helvetica, sans-serif;
}

JS:

var board = [["1", "1", "2", "2", "3", "3"],
["4", "4", "5", "5", "6", "6"],
["7", "7", "8", "8", "9", "9"],
["10", "10", "11", "11", "12", "12"],
["13", "13", "14", "14", "15", "15"],
["16", "16", "17", "17", "18", "18"]];



function drawBoard() {
for (int row = 0; row < board.length; i++) {
for (int col = 0; col < board[row].length; j++) {
var cell = document.getElementById("cell" + row + col);
cell.innerHTML = board[row][col];
}

}
}

最佳答案

所以我在浏览器中运行了你的代码,它输出了一些我修复的错误。这是工作代码:

var board = [["1", "1", "2", "2", "3", "3"],
["4", "4", "5", "5", "6", "6"],
["7", "7", "8", "8", "9", "9"],
["10", "10", "11", "11", "12", "12"],
["13", "13", "14", "14", "15", "15"],
["16", "16", "17", "17", "18", "18"]];



function drawBoard() {
for (var row = 0; row < board.length; row++) {
for (var col = 0; col < board[row].length; col++) {
var cell = document.getElementById("cell" + row + col);
cell.innerHTML = board[row][col];
}

}
}

我修复的内容是:

  • 您在 for 循环中使用 int 而不是 var 来声明 row/col。 JavaScript 没有任何类型。
  • 您增加的是 i/j 而不是 row/col。

关于javascript - 将二维数组输出到html文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42192412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com