Insert HTML element two (or more) times using JavaScript

后端 未结 3 919
执念已碎
执念已碎 2020-12-06 19:55

How do I insert a HTML element twice using JavaScript?

This code isn\'t working:

var div = document.createElement(\'div\');
div.innerText = \'Hello!\         


        
3条回答
  •  失恋的感觉
    2020-12-06 20:26

    Ok I found that you just have to use cloneNode:

    var div = document.createElement('div');
    div.innerText = 'Hello!';
    document.body.appendChild(div);
    var div2 = div.cloneNode(true);
    document.body.appendChild(div2);
    

提交回复
热议问题