我正在学习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
。我做错了什么?