admin管理员组

文章数量:1305232

I tried to use cloneNode mentionned here Copy the content of one table into another but Chrome says cloneNode is not a function

/

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

script:

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

I tried to use cloneNode mentionned here Copy the content of one table into another but Chrome says cloneNode is not a function

https://jsfiddle/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>

script:

myTable = document.getElementsByTagName("Table")[0];
myClone = myTable.cloneNode(true);
document.body.appendChild(myClone);
Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Jan 31, 2016 at 13:47 user310291user310291 38.2k88 gold badges294 silver badges518 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The getElementsByTagName() method accesses all elements with the specified tagname.So you have to select the first element of the NodeList. So passed [0] to select it.

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

WORKING FIDDLE

本文标签: htmlHow to clone a whole table in JavascriptStack Overflow