2016-01-31 20 views
1

我試圖用cloneNode mentionned這裏Copy the content of one table into another而Chrome說cloneNode不是一個函數如何在Javascript中克隆整個表格?

https://jsfiddle.net/4wczdykc/1/

<table> 
    <thead> 

     <tr> 
      <th scope="col" colspan="1">TABLE TO CLONE</th> 
     </tr> 

     <tr> 
      <th>Column</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td></td>       
     </tr> 
    </tbody> 
</table> 

腳本:

myTable = document.getElementsByTagName("Table")[0]; 
myClone = myTable.cloneNode(true); 
document.body.appendChild(myClone); 

回答

3

getElementsByTagName()方法訪問與指定的標記的所有元素。所以你必須選擇NodeList的第一個元素。所以通過[0]選擇它。

myTable = document.getElementsByTagName("table")[0]; 
    myClone = myTable.cloneNode(true); 
    document.body.appendChild(myClone); 

WORKING FIDDLE