最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to clone a whole table in Javascript? - Stack Overflow

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论