我有一个多维数组,并非所有元素都会被填充。我想将该数组的内容输出到 HTML 表中。我目前有这个:
for (var basketRowJuice = 0; basketRowJuice < 10; basketRowJuice++){
var outputName = "";
outputName = (basketArray[0][basketRowJuice]);
document.getElementById("basketJuiceName" + basketRowJuice).innerHTML = outputName;
}
for (var basketRowQuant = 0; basketRowQuant < 10; basketRowQuant++){
var outputQuant = 0;
outputQuant = (basketArray[1][basketRowQuant]);
document.getElementById("basketJuiceQuant" + basketRowQuant).innerHTML = outputQuant;
}
for (var basketRowPrice = 0; basketRowPrice < 10; basketRowPrice++){
var outputPrice = 0;
outputPrice = (basketArray[2][basketRowPrice]);
document.getElementById("basketJuicePrice" + basketRowPrice).innerHTML = outputPrice;
}
HTML表格:
<table id="basket" cellpadding="0" cellspacing="0" summary="This table lists the items you have in your shopping basket">
<caption>Your basket</caption>
<tr>
<th scope="col">Juice name</th>
<th scope="col">Number of crates</th>
<th scope="col">Total price</th>
</tr>
<tr>
<td id="basketJuiceName0"></td>
<td id="basketJuiceQuant0"></td>
<td id="basketJuicePrice0"></td>
</tr>
<tr>
<td id="basketJuiceName1"></td>
<td id="basketJuiceQuant1"></td>
<td id="basketJuicePrice1"></td>
</tr>
<tr>
<td id="basketJuiceName2"></td>
<td id="basketJuiceQuant2"></td>
<td id="basketJuicePrice2"></td>
</tr>
<tr>
<td id="basketJuiceName3"></td>
<td id="basketJuiceQuant3"></td>
<td id="basketJuicePrice3"></td>
</tr>
<tr>
<td id="basketJuiceName4"></td>
<td id="basketJuiceQuant4"></td>
<td id="basketJuicePrice4"></td>
</tr>
<tr>
<td id="basketJuiceName5"></td>
<td id="basketJuiceQuant5"></td>
<td id="basketJuicePrice5"></td>
</tr>
<tr>
<td id="basketJuiceName6"></td>
<td id="basketJuiceQuant6"></td>
<td id="basketJuicePrice6"></td>
</tr>
<tr>
<td id="basketJuiceName7"></td>
<td id="basketJuiceQuant7"></td>
<td id="basketJuicePrice7"></td>
</tr>
<tr>
<td id="basketJuiceName8"></td>
<td id="basketJuiceQuant8"></td>
<td id="basketJuicePrice8"></td>
</tr>
<tr>
<td id="basketJuiceName9"></td>
<td id="basketJuiceQuant9"></td>
<td id="basketJuicePrice9"></td>
</tr>
</table>
但是,如果我的数组仅填充在 [0][9]、[1][9] 和 [2][9] 列中,那么在输出之前,我的 HTML 中会出现巨大的空行间隙。谁能建议一个更好的方法来做到这一点?也许这样我的 javascript 会为每个填充的数组元素插入一个表格行?
我是编程新手,所以请原谅我毫无疑问的冗长和不优雅的代码。
谢谢你,乔恩