Remove <b> tag from text

旧时模样 提交于 2019-12-14 02:47:06

问题


I want to make a bold text non-bold using Greasemonkey. I have only found ways to remove the tag along with its text, but not a way to simply remove the tag itself.

So how do I make

<b>
some text
</b>

just

some text

?


回答1:


Assuming you've only one TextNode inside each B tag.

b.parentNode.replaceChild(b.firstChild, b);

Example : http://jsfiddle.net/DGTh5/




回答2:


Assuming you're dealing with elements, not a string:

function unwrapChildren(element) {
    var parent, node, nextNode;

    parent = element.parentNode;
    for (node = element.firstChild; node; node = nextNode) {
         nextNode = node.nextSibling;
         parent.insertBefore(node, element);
    }
    parent.removeChild(element);
}

Then call unwrapChildren with the b element. The above will move all of its child nodes (there's probably only one, a text node) into the b element's parent node where the b element is, and then remove the b element.



来源:https://stackoverflow.com/questions/18851776/remove-b-tag-from-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!