克隆节点

Tutorial: DOM实战 Category: JS Published: 2026-04-07 13:58:26 Views: 20 Likes: 0 Comments: 0
<!DOCTYPE html>
<html lang="en">
  <head></head>

  <body>
    <ul>
      <li>1111</li>
      <li>2</li>
      <li>3</li>
    </ul>

    <script>
      var ul = document.querySelector("ul");

      // 1. node.cloneNode(); 括号为空或者里面是false 浅拷贝 只复制标签不复制里面的内容
      // 2. node.cloneNode(true); 括号为true 深拷贝 复制标签复制里面的内容
      var lili1 = ul.children[0].cloneNode();
      var lili2 = ul.children[0].cloneNode(true);

      ul.appendChild(lili1); // <li></li>
      ul.appendChild(lili2); // <li>1111</li>
    </script>
  </body>
</html>