请问一下JavaScript 里创建div和table的问题

来源:百度知道 编辑:UC知道 时间:2024/06/18 07:31:01
我想请问一下就是 我需要创建一个div 然后再在这个div里创建一个table

var oDiv = document.createElement("DIV");
oDiv.id = "carInfo";
oDiv.style.top = 200;
oDiv.style.left = 200;
oDiv.style.background = '#FFFF00';
oDiv.style.visibility = 'visible';
oDiv.innerHTML="123123"
document.body.appendChild(oDiv);

比如我这样创建好一个div以后 我需要再在这里面创建table的话 是不是接着 createElement table呢 当然这个table得属于这个div
var oTable = document.createElement("DIV");
oTable.id = "carInfoTable"
document.getElementById("carInfoDiv").appendChild(oDiv);

我想着是这么写的 但是
document.getElementById("carInfoDiv").appendChild(oDiv);

这里出错了
uncaught exception: Node cannot be inserted at the specified point in the hierarchy (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)

直接createElement就可以。然后直接oDiv.appendChild(table)。
但注意,操作table创建tr的时候,最好使用insertRow,否则很容易出错,因为在ie下,是要求tbody对象下才可以appendChild(tr)的。