我已经尝试了通过谷歌可以找到的所有内容,但没有任何效果正常。输出只是一行,列出了数组的所有内容。我需要的是一种写入数组内容的方法,但在 3 个单元格之后,自动开始一个新行。我将发布我在下面编写的代码以及问题。 (是的,这是来自一项作业。:( )
//***(8) place the words in the string "tx_val" in a table with a one pixel border,
//*** with a gray backgound. Use only three cells per row. Empty cells should contain
//*** the word "null". Show the table in the span block with id="ans8"
var count = i % 3;
var nrow = "";
var out = "<table border='1' bgcolor='gray'><tr>"
for (var i=0; i<txArr.length; i++)
{
out += ("<td>" + txArr[i] + "</td>");
count++;
if (count % 3 == 0)
{
nrow += "</tr><tr>";
}
}
document.getElementById('ans8').innerHTML = out + nrow;
最佳答案
您需要打印tr
位于表格内(并添加 </table>
!):
var count = i % 3; // btw. what's this??
var nrow = "";
var out = "<table border='1' bgcolor='gray'><tr>"
for (var i=0; i<txArr.length; i++)
{
out += "<td>" + txArr[i] + "</td>";
count++;
if (count % 3 == 0)
out += "</tr><tr>";
}
out += "</table>";
document.getElementById('ans8').innerHTML = out;
关于javascript - 需要 JavaScript 和从数组创建 html 表的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5795273/