漸漸地工作自學JS與一對夫婦的書,現在我在陣列。如果這個問題不符合標準,請忽略,我將刪除。 我確定可以在用戶輸入時將數組的元素同時輸出到HTML表格,但我已經用盡了想法並用盡了搜索這個網站的答案。我現在有一個3x3 HTML表格,一個2d數組(「數組數組」)。要提示用戶輸入一個正整數並將其加載到每個相應的單元格()位置。最終,我將編寫代碼來測試行,列和對角線的總和是否相同。尋找一些我需要修改當前代碼/函數的指針。這是我到目前爲止。欣賞評論。作爲用戶輸入數字到HTML表格中,我可以輸出2d數組的元素嗎?
<html>
<head>
<script type="text/javascript">
function simpleTable() {
//create array of 3 rows 3 columns
var myArray = new Array(3);
for(var i = 0; i < 3; i++)
{
myArray[i] = new Array(3);
}
for(var i = 0; i < 3; i++) //prompt user for input and fill the array
{
for(var j = 0; j < 3; j++)
{
myArray[i][j]= prompt("Enter a number for row " + (i + 1) +
", column " + (j + 1) + ":");
//need to output user input immediately to a cell in HTML
table here.
}
}
}
//add another function here to test the rows, cols diagonals if sums
are equal
function areEqual() {
//expand after solving the issue with seeing user input data
}
</script>
</head>
<body>
<div id="button1">
<button type="button" id="nums" onClick="simpleTable()">Enter
positive numbers</button>
</div>
<table width="100%" border="1">
<tr>
<td height="50"><span id="c01" /span></td>
<td><span id="c02" /span></td>
<td><span id="c03" /span></td>
</tr>
<tr>
<td height="50"><span id="c1l" /span></td>
<td><span id="c12" /span></td>
<td><span id="c13" /span></td>
</tr>
<tr>
<td height="50"><span id="c21" /span></td>
<td><span id="c22" /span></td>
<td><span id="c23" /span></td>
</tr>
</table>
</body>
</html>
謝謝!在之前的場景中,我動態創建了表格,並且效果更好。有時候,這些書本身並沒有那麼有用,特別是章節末尾的所謂場景。我將不得不閱讀這些評論以準確理解我在做什麼。儘管一如既往的讚賞。一點一點地學習。 – allendks45 2015-04-04 19:42:41