<html><head><title></title>
<script>
function createTable(){
var newTable=document.createElement("table");var newRow=document.createElement("tr");var newCell=document.createElement("td");var newText=document.createTextNode("hello world");newCell.appendChild(newText);newRow.appendChild(newCell);newTable.appendChild(newRow);document.body.appendChild(newTable);}
</script>
</head><body onload="createTable()"></body></html>
我想知道我这样创建不成功的原理,不是想要能成功的代码 !

解决方案 »

  1.   

    <title></title>
    <script>
    function createTable(){
    var newTable=document.createElement("table");
    var newRow=newTable.insertRow(-1);
    var newCell=newRow.insertCell(-1);
    var newText=document.createTextNode("Hello 老石头");
    newCell.appendChild(newText);
    document.body.appendChild(newTable);
    }
    </script>
    </head>
    <body onload="createTable()">
    </body>
    </html>
      

  2.   

    创建tab 一定要有tbody
    <html> <head> <title> </title> 
    <script> 
    function createTable(){ 
    var newTable=document.createElement("table"); var tbody =document.createElement("tbody"); var newRow=document.createElement("tr"); var newCell=document.createElement("td"); var newText=document.createTextNode("hello world"); newCell.appendChild(newText); newRow.appendChild(newCell);
     
    tbody.appendChild(newRow)newTable.appendChild(tbody); document.body.appendChild(newTable); 

    </script> 
    </head> <body onload="createTable()"> </body> 
      

  3.   

    同意楼上。
    我们平时在html页面中使用table标记时,一般会省略tbody标记,浏览器能够识别并且不会报错,但浏览器会自动处理,为tr标记外包裹tbody标记。