2014-11-08 2 views
0

"click me!"버튼을 클릭 한 후 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/