<html>
<body>
<script language="javascript">
var table = document.createElement("table");
row = table.insertRow();
col = row.insertCell();
col.setAttribute("width",100);
col.innerText="这个是单元格";
document.body.appendChild(table);
alert(document.body.lastChild.outerHTML)
</script>
</body>
</html>
<body>
<script language="javascript">
var table = document.createElement("table");
row = table.insertRow();
col = row.insertCell();
col.setAttribute("width",100);
col.innerText="这个是单元格";
document.body.appendChild(table);
alert(document.body.lastChild.outerHTML)
</script>
</body>
</html>
document.body.insertAdjacentHTML("BeforeEnd",str);
我是想用document.createElement来创建节点,
但我用document.createElement创建的节点都不能显示,但肯定添加进去了
<body>
<script language="javascript">
var table = document.createElement("table");
var tbody=document.createElement("tbody");
row = document.createElement("tr");
col = document.createElement("td");
col.setAttribute("width",100);
col.appendChild(document.createTextNode("这个是单元格"));
row.appendChild(col);
tbody.appendChild(row);
table.appendChild(tbody);
document.body.appendChild(table);
</script>
</body>
</html>
还有两点想问明白
一、动态生成table节点必须要tbody标签?
二,如果给表格添加背景色的话添加不上,代码如下<html>
<body>
<script language="javascript">
var table = document.createElement("table");
table.setAttribute("bgcolor","red"); //这里是添加背景代码
var tbody=document.createElement("tbody");
row = document.createElement("tr");
col = document.createElement("td");
col.setAttribute("width",100);
col.appendChild(document.createTextNode("这个是单元格"));
row.appendChild(col);
tbody.appendChild(row);
table.appendChild(tbody);
document.body.appendChild(table);
</script>
</body>
</html>
-->
table.setAttribute("bgColor","red"); //这里是添加背景代码