迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

使用 JavaScript 删除节点的父元素

作者:迹忆客 最近更新:2022/12/21 浏览次数:

删除节点的直接父元素

要删除节点的父元素:

  1. 使用 document.getElementById() 方法选择子节点。
  2. 使用 parentElement 属性来访问父元素。
  3. 在父级上调用 remove() 方法,例如 child.parentElement.remove()

这是此示例的 HTML。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
  </head>

  <body>
    <!-- 👇️ want to remove this div  -->
    <div>
      <div id="child">Child 1</div>
    </div>

    <script src="index.js"></script>
  </body>
</html>

这是相关的 JavaScript 代码。

const child = document.getElementById('child');

child.parentElement.remove();

Node.parentElement 属性返回 DOM 节点的父元素。

如果节点没有父节点,则 parentElement 属性返回 null。

如果这是我们需要处理的事情,我们可以在调用 remove() 方法之前有条件地检查。

const child = document.getElementById('child');

child?.parentElement?.remove();

如果元素没有父元素,我们使用可选的链接 (?.) 运算符来短路而不是抛出错误。

请注意 ,如果要删除的元素不是直接父元素,则可以多次访问 parentElement 属性。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
  </head>

  <body>
    <!-- 👇️ Want to remove this Div -->
    <div>
      <span>
        <div id="child">Child 1</div>
      </span>
    </div>

    <script src="index.js"></script>
  </body>
</html>

这是相关的 JavaScript 代码。

const child = document.getElementById('child');

child.parentElement.parentElement.remove();

我们访问了 parentElement 属性两次以找到我们想要删除的 div 元素。

如果我们需要删除父元素而不删除其子元素,则可以改用这种方法。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
  </head>

  <body>
    <div style="background-color: salmon">
      <div id="child">Child 1</div>
      <div>Child 2</div>
      <div>Child 3</div>
    </div>

    <script src="index.js"></script>
  </body>
</html>

这是相关的 JavaScript 代码。

const child = document.getElementById('child');

child.parentElement.replaceWith(...child.parentElement.childNodes);

要删除没有子节点的父节点,我们基本上用它的子节点替换父节点。

删除节点的间接父元素

要删除节点的间接父元素:

  1. 使用 document.getElementById() 方法选择子节点。
  2. 使用 closest() 方法选择间接父元素。
  3. 调用父级的 remove() 方法将其删除。
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
  </head>

  <body>
    <!-- 👇️ want to remove this div -->
    <div class="container">
      <div>
        <span>
          <div id="child">Child 1</div>
        </span>
      </div>
    </div>

    <script src="index.js"></script>
  </body>
</html>

这是相关的 JavaScript 代码。

const child = document.getElementById('child');

const parent = child.closest('div.container');

parent.remove();

我们使用了 closest 方法而不是 parentElement 属性,因为我们试图删除一个不是直接父元素的元素。

如果元素本身与选择器匹配,则返回该元素。

如果不存在与提供的选择器匹配的元素,则 closest() 方法返回 null。

该方法采用包含选择器的字符串。 提供的选择器可以根据需要指定。

如果向 closest() 方法提供了无效的选择器字符串,则会抛出 SyntaxError

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便