问题
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