[英]How to remove an element from a string version of html
Is there an easy way to remove HTML elements from a string?有没有一种简单的方法可以从字符串中删除 HTML 元素? I would like to remove all the
del
elements from this string:我想从此字符串中删除所有
del
元素:
<p>one</p>
<p><del>add</del><ins>four</ins><del>add</del></p>
<p>five</p>
Use DOMParser to turn it into a document, iterate over all del
elements in the document and remove them.使用 DOMParser 将其转换为文档,遍历文档中的所有
del
元素并删除它们。 Then turn it back into a string:然后将其转回字符串:
const str = `<p>one</p> <p><del>add</del><ins>four</ins><del>add</del></p> <p>five</p>`; const doc = new DOMParser().parseFromString(str, 'text/html'); for (const del of doc.querySelectorAll('del')) { del.remove(); } console.log(doc.body.innerHTML);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.