2014-11-08 92 views
0

我想插入动态创建的表格到div元素后按钮“点击我!”按下。插入表格到div元素

这代码:

<body> 
    <button onclick="CreatTable()">click me!</button> 
    <div id="ShowDataID"></div> 
    <script type="text/javascript"> 
     var data = ['First Name', 'Last Name', 'Email']; 

     function CreatTable() { 

      var table = document.createElement('table'); 
      //some logic to fill table 
      document.getElementById('ShowDataID').innerHTML = table; 
} 
    </script> 
</body> 

但经过我按 “点击我!”按钮表不显示。

为什么表不显示我在这里失踪?

回答

2
#1 
function CreatTable() { 
    var table = document.createElement('table'); 

    document.getElementById('ShowDataID').appendChild(table); 
} 

#2 
function CreatTable() { 
    var _tmp = document.createElement('div'), 
     table = document.createElement('table'); 

    _tmp.appendChild(table); 

    document.getElementById('ShowDataID').innerHTML = _tmp.innerHTML; 
} 

http://jsbin.com/soceca/1/