<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>

解决方案 »

  1.   

    var str = "<table><tr><td width=100>这个是单元格</td></tr></table>";
    document.body.insertAdjacentHTML("BeforeEnd",str);
      

  2.   

    大家误解我的意思了
    我是想用document.createElement来创建节点,
    但我用document.createElement创建的节点都不能显示,但肯定添加进去了
      

  3.   

    <html>
    <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>
      

  4.   

    谢谢飞天
    还有两点想问明白
    一、动态生成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>
      

  5.   

    table.setAttribute("bgcolor","red");            //这里是添加背景代码
    -->
    table.setAttribute("bgColor","red");            //这里是添加背景代码