简体   繁体   English

将表格插入div元素

[英]Insert table to div element

I want to insert dynamically created table to the div element after button "click me!" 我想在按钮“单击我!”之后将动态创建的表插入div元素。 pressed. 按下。

Here it the code: 这里是代码:

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

But after I press "click me!" 但是当我按下“点击我!”之后 button table not displayed. 按钮表未显示。

Why table not displayed what I am missing here? 为什么表格未显示我在这里缺少的内容?

#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/ http://jsbin.com/soceca/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM