【发布时间】:2011-10-15 13:42:03
【问题描述】:
我正在学习 appendChild 并且到目前为止想出了这个代码:
var blah = "Blah!";
var t = document.createElement("table"),
tb = document.createElement("tbody"),
tr = document.createElement("tr"),
td = document.createElement("td");
t.style.width = "100%";
t.style.borderCollapse = 'collapse';
td.appendChild(document.createTextNode(blah));
// note the reverse order of adding child
tr.appendChild(td);
tb.appendChild(tr);
t.appendChild(tb);
document.getElementById("theBlah").appendChild(t);
<div id="theBlah">d</div>
但这给了我一个错误,说Uncaught TypeError: Cannot call method 'appendChild' of null。我做错了什么?
【问题讨论】:
-
你也可以把你的脚本放在 theBlah 下面,它可以在没有 onload 监听器的情况下工作。