{"id":1290,"date":"2024-11-10T12:01:24","date_gmt":"2024-11-10T04:01:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1290\/"},"modified":"2024-11-10T12:01:24","modified_gmt":"2024-11-10T04:01:24","slug":"%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e5%8e%bb%e9%99%a4js%e4%b8%ad%e7%9a%84html%e6%a0%87%e7%ad%be%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1290\/","title":{"rendered":"\u5982\u4f55\u9ad8\u6548\u53bb\u9664JS\u4e2d\u7684HTML\u6807\u7b7e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173035586852805.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u9ad8\u6548\u53bb\u9664JS\u4e2d\u7684HTML\u6807\u7b7e\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u9ad8\u6548\u53bb\u9664JS\u4e2d\u7684HTML\u6807\u7b7e\uff1f\u63d2\u56fe\" \/><\/p>\n<p>js\u53bb\u9664html\u6807\u7b7e\u662f\u4e00\u9879\u975e\u5e38\u91cd\u8981\u7684\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u4ece\u4e0d\u53ef\u9760\u6765\u6e90\u83b7\u5f97\u7684\u6570\u636e\u65f6\u3002\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6b64\u64cd\u4f5c\u3002<\/p>\n<p>\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5b83\u662f\u4e00\u79cd\u4e13\u95e8\u7528\u4e8e\u5bfb\u627e\u548c\u66ff\u6362\u6587\u672c\u6a21\u5f0f\u7684\u5f3a\u5927\u5de5\u5177\u3002\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u9664html\u6807\u7b7e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>const text = \"&lt;h1&gt;this is a heading&lt;\/h1&gt;&lt;p&gt;this is a paragraph&lt;\/p&gt;\";\nconst result = text.replace(\/&lt;[\/]?.+?&gt;\/g, \"\");\nconsole.log(result); \/\/ \u8f93\u51fa: this is a headingthis is a paragraph<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u6709\u6548\uff0c\u4f46\u5b83\u4e5f\u53ef\u80fd\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u51fa\u73b0\u95ee\u9898\uff0c\u4f8b\u5982\u5f53html\u6807\u7b7e\u4e2d\u5305\u542b\u5d4c\u5957\u5185\u5bb9\u65f6\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u53e6\u4e00\u79cd\u53bb\u9664html\u6807\u7b7e\u7684\u65b9\u6cd5\u662f\u4f7f\u7528dom\u89e3\u6790\u5668\u3002dom\u89e3\u6790\u5668\u662f\u4e00\u79cd\u4e13\u95e8\u7528\u4e8e\u64cd\u4f5chtml\u6587\u6863\u7684\u5de5\u5177\u3002\u8981\u4f7f\u7528dom\u89e3\u6790\u5668\u53bb\u9664html\u6807\u7b7e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>const parser = new DOMParser();\nconst doc = parser.parseFromString(text, \"text\/html\");\nconst result = doc.body.textContent;\nconsole.log(result); \/\/ \u8f93\u51fa: This is a headingThis is a paragraph<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u66f4\u4e3a\u5065\u58ee\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6b63\u786e\u5904\u7406\u5d4c\u5957html\u6807\u7b7e\u3002\u4f46\u662f\uff0c\u5b83\u4e5f\u6bd4\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5\u6162\u3002<\/p>\n<p>\u6839\u636e\u5177\u4f53\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5\u6216dom\u89e3\u6790\u5668\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u9ad8\u6548\u53bb\u9664JS\u4e2d\u7684HTML\u6807\u7b7e\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>js\u53bb\u9664html\u6807\u7b7e\u662f\u4e00\u9879\u975e\u5e38\u91cd\u8981\u7684\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u4ece\u4e0d\u53ef\u9760\u6765\u6e90\u83b7\u5f97\u7684\u6570\u636e\u65f6\u3002\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6b64\u64cd\u4f5c\u3002 \u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5b83\u662f\u4e00\u79cd\u4e13\u95e8\u7528\u4e8e\u5bfb\u627e\u548c\u66ff\u6362\u6587\u672c\u6a21\u5f0f\u7684\u5f3a\u5927\u5de5\u5177\u3002\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u9664html\u6807\u7b7e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a const text = &#8220;&lt;h1&gt;this is a heading&lt;\/h1&gt;&lt;p&gt;this is a paragraph&lt;\/p&gt;&#8221;; const result = text.replace(\/&lt;[\/]?.+?&gt;\/g, &#8220;&#8221;); console.log(result); \/\/ \u8f93\u51fa: this is a headingthis is a paragraph \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u6709\u6548\uff0c\u4f46\u5b83\u4e5f\u53ef\u80fd\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u51fa\u73b0\u95ee\u9898\uff0c\u4f8b\u5982\u5f53html\u6807\u7b7e\u4e2d\u5305\u542b\u5d4c\u5957\u5185\u5bb9\u65f6\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u53e6\u4e00\u79cd\u53bb\u9664html\u6807\u7b7e\u7684\u65b9\u6cd5\u662f\u4f7f\u7528dom\u89e3\u6790\u5668\u3002dom\u89e3\u6790\u5668\u662f\u4e00\u79cd\u4e13\u95e8\u7528\u4e8e\u64cd\u4f5chtml\u6587\u6863\u7684\u5de5\u5177\u3002\u8981\u4f7f\u7528dom\u89e3\u6790\u5668\u53bb\u9664html\u6807\u7b7e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a const parser = new DOMParser(); const doc = parser.parseFromString(text, &#8220;text\/html&#8221;); const result = doc.body.textContent; console.log(result); \/\/ \u8f93\u51fa: This is a headingThis is a paragraph [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1290","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1290"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1290\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}