使用 JavaScript 删除节点的父元素
删除节点的直接父元素
要删除节点的父元素:
-
使用
document.getElementById()
方法选择子节点。 -
使用
parentElement
属性来访问父元素。 -
在父级上调用
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);
要删除没有子节点的父节点,我们基本上用它的子节点替换父节点。
删除节点的间接父元素
要删除节点的间接父元素:
-
使用
document.getElementById()
方法选择子节点。 -
使用
closest()
方法选择间接父元素。 -
调用父级的
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
。
相关文章
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 中合并两个数组而不出现重复的情况
发布时间:2024/03/23 浏览次数:86 分类:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。