JS - 删除标记而不删除内容 [英] JS - Remove a tag without deleting content

查看:116
本文介绍了JS - 删除标记而不删除内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以移除标签但是保留内容?例如,是否可以删除SPAN标记但是在那里留下SPAN的内容?

I am wondering if it is possible to remove a tag but leave the content in tact? For example, is it possible to remove the SPAN tag but leave SPAN's content there?

<p>The weather is sure <span>sunny</span> today</p> //original

<p>The weather is sure sunny today</p> //turn it into this

我尝试过使用这个使用replaceWith()的方法,但它将HTML转换为

I have tried using this method of using replaceWith(), but it it turned the HTML into

<p>
  "The weather is sure " 
  "sunny"
  " today"
</p>

编辑:测试完所有答案后,我意识到我的代码有问题。我不断获得三个分割文本节点的原因是由于插入了SPAN标记。我将创建另一个问题来尝试解决我的问题。

EDIT : After testing all of your answers, I realized that my code is at fault. The reason why I keep getting three split text nodes is due to the insertion of the SPAN tag. I'll create another question to try to fix my problem.

推荐答案

jQuery有更简单的方法:

jQuery has easier ways:

var spans = $('span');
spans.contents().unwrap();

使用不同的选择器方法,可以删除深层嵌套的跨度或仅直接导入元素的子跨度。

With different selector methods, it is possible to remove deeply nested spans or just direct children spans of an element.

这篇关于JS - 删除标记而不删除内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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