JavaScript & copy style

前端 未结 3 1468
南笙
南笙 2020-12-19 04:39

I am copying a table cell with javascript.

It works fine, just that it doesn\'t copy the style. I wanted to copy like below, but that didn\'t work. newCell.style=old

3条回答
  •  轮回少年
    2020-12-19 05:15

    You can copy a DOM Element with all its content (including attributes) with .cloneNode(true) :

    var clonedTr = document.getElementById('id').cloneNode(true);
    

    Then clonedTr is an exact copy of the tr #id. The "true" means you want to copy the content of the element.

提交回复
热议问题