复制dom

mdn Node.cloneNode() 复制节点的时候 不会复制 addEventListener和dom.onclick 绑定的事件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="wrap">
        wrap
        <input type="text" value="123">
        <div class="content">
            content
        </div>
    </div>
    <script>
        const wrap = document.querySelector('.wrap')
        wrap.addEventListener('click',function(){
            console.log('wrap click from add')
        })
        wrap.onclick = function(){
            console.log('wrap click from on')
        }
        const content = document.querySelector('.content')
        const wrap_clone = wrap.cloneNode(true)
        document.body.appendChild(wrap_clone)
    </script>
</body>
</html>
posted @ 2020-04-06 10:43  菜鸡_chicken  阅读(268)  评论(0编辑  收藏  举报