我想操作一个 html 字符串,在 vanillaJS 中我会这样做:
let HTMLString = '<p>hello</p>
<style>
p{
color:black
}
<p>world</p>
'
const parser = new DOMParser();
const doc = parser.parseFromString(HTMLString, "text/html");
doc.querySelectorAll('style').forEach(item => item.remove())
Run Code Online (Sandbox Code Playgroud)
这也适用于反应,但我相信,由于下一步编译服务器端,它只会抛出DOMParser is not defined,我尝试过像这样的包html-react-parser,它们只是将html解析为反应元素,然后提供非常有限的功能,我最后的手段是使用正则表达式,但我认为这不是一个好主意。
任何帮助都会很好,谢谢
有时控制台在打开网站时会显示这些错误
\nGET https://example.com/subpath/_next/static/9ufj5kFJf/_buildManifest.js\n[HTTP/3 404 Not Found 311ms]\n\nGET https://example.com/subpath/_next/static/9ufj5kFJf/_ssgManifest.js\n[HTTP/3 404 Not Found 334ms]\n\nLoading failed for the <script> with source \xe2\x80\x9chttps://example.com/subpath/_next/static/9ufj5kFJf/_buildManifest.js\xe2\x80\x9d. 1434-247:1:1\nLoading failed for the <script> with source \xe2\x80\x9chttps://example.com/subpath/_next/static/9ufj5kFJf/_ssgManifest.js\xe2\x80\x9d.\nRun Code Online (Sandbox Code Playgroud)\n该应用程序确实使用了 ISR,并且似乎有效,它确实得到了更新,这些文件有什么作用?如果他们失踪了会发生什么?
\n