我想知道是否可以删除标签但保留内容?例如,是否可以删除 SPAN 标签但保留 SPAN 的内容?
<p>The weather is sure <span>sunny</span> today</p> //original
<p>The weather is sure sunny today</p> //turn it into this
我试过使用 this使用 replaceWith() 的方法,但它把 HTML 变成了
<p>
"The weather is sure "
"sunny"
" today"
</p>
编辑:在测试了您的所有答案后,我意识到我的代码有问题。我一直得到三个拆分文本节点的原因是由于插入了 SPAN 标记。我将创建另一个问题来尝试解决我的问题。
最佳答案
<p>The weather is sure <span>sunny</span> today</p>;
var span=document.getElementsByTagName('span')[0]; // get the span
var pa=span.parentNode;
while(span.firstChild) pa.insertBefore(span.firstChild, span);
pa.removeChild(span);
关于javascript - JS - 删除标签而不删除内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9848465/