如何在 JavaScript 中仅删除父元素而不删除其子元素? [英] How to remove only the parent element and not its child elements in JavaScript?

查看:31
本文介绍了如何在 JavaScript 中仅删除父元素而不删除其子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说:

前文<div class="remove-just-this"><p>孩子 foo</p><p>子栏</p>嵌套文本

发布文本

为此:

前文<p>孩子 foo</p><p>子栏</p>嵌套文本发布文本

我一直在想办法使用 Mootools、jQuery 甚至(原始)JavaScript,但不知道如何做到这一点.

解决方案

使用 jQuery 你可以做到:>

var cnt = $(".remove-just-this").contents();$(".remove-just-this").replaceWith(cnt);

文档的快速链接:

Let's say:

<div>
  pre text
  <div class="remove-just-this">
    <p>child foo</p>
    <p>child bar</p>
    nested text
  </div>
  post text
</div>

to this:

<div>
  pre text
  <p>child foo</p>
  <p>child bar</p>
  nested text
  post text
</div>

I've been figuring out using Mootools, jQuery and even (raw) JavaScript, but couldn't get the idea how to do this.

解决方案

Using jQuery you can do this:

var cnt = $(".remove-just-this").contents();
$(".remove-just-this").replaceWith(cnt);

Quick links to the documentation:

这篇关于如何在 JavaScript 中仅删除父元素而不删除其子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆