节点操作之删除节点

蔚落 2023-10-03 15:21 179阅读 0赞

节点操作之删除节点

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>节点操作之删除节点</title>
  8. </head>
  9. <body>
  10. <button>删除</button>
  11. <ul>
  12. <li>熊大</li>
  13. <li>熊二</li>
  14. <li>光头强</li>
  15. </ul>
  16. <script>
  17. // 1.获取元素
  18. var ul = document.querySelector('ul');
  19. var btn = document.querySelector('button');
  20. // 2. 删除元素 node.removeChild(child)
  21. // ul.removeChild(ul.children[0]);
  22. // 3. 点击按钮依次删除里面的孩子
  23. btn.onclick = function () {
  24. if (ul.children.length == 0) {
  25. this.disabled = true; //disable当为0的时候 删除按钮禁用
  26. }
  27. else {
  28. ul.removeChild(ul.children[0]);//移除ul里边的第0个孩子
  29. }
  30. }
  31. </script>
  32. </body>
  33. </html>

发表评论

表情:
评论列表 (有 0 条评论,179人围观)

还没有评论,来说两句吧...

相关阅读

    相关 DOM删除节点

    一 介绍 删除节点通过使用removeChild()方法来实现。 removeChild()方法用来删除一个子节点。 obj. removeChild(oldChild)