[英]Generate a 4 column table from javascript array data
My question is very similar to How to Generate 3x3 HTML Table From an JavaScript Array the only problem is that the answer I prefer in that thread I cant seem to make work (I like it because it does not use innerHTML). 我的问题与如何从JavaScript数组生成3x3 HTML表非常相似,唯一的问题是我希望在该线程中得到的答案我似乎无法完成工作(我喜欢它,因为它不使用innerHTML)。
My current code: 我当前的代码:
var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var table = document.createElement("table");
var i = 0;
for (var r = 0; r < 4; r++) {
var row = table.insertRow();
for (var c= 0; c < 4; c++) {
var cell = row.insertCell();
cell.appendChild(document.createTextNode(alphabet[i++]));
}
}
document.body.appendChild(table);
Basically when I include this .js file in my html document nothing happens. 基本上,当我在HTML文档中包含此.js文件时,没有任何反应。 I tried creating a table and loading it inside of it also, but that did nothing. 我尝试创建一个表并将其加载到表中,但这没有任何作用。 I am probably missing something obvious here. 我可能在这里缺少明显的东西。
Try 尝试
window.onload = function() { var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]; var table = document.createElement("table"); var i = 0; for (var r = 0; r < 4; r++) { var row = table.insertRow(r); for (var c = 0; c < 4; c++) { var cell = row.insertCell(c); cell.appendChild(document.createTextNode(alphabet[i++])); } } document.body.appendChild(table); }
Don't just include, put this in onload 不只是包括,将其放在onload
<script type="text/javascript">
function load()
{
// your code here
}
</script>
</head>
<body onload="load()">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.