{"id":25996,"date":"2024-11-24T15:25:40","date_gmt":"2024-11-24T07:25:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25996\/"},"modified":"2024-11-24T15:25:40","modified_gmt":"2024-11-24T07:25:40","slug":"%e5%a6%82%e4%bd%95%e4%bb%8e-html-%e6%a0%87%e7%ad%be%e4%b8%ad%e5%8e%bb%e9%99%a4%e6%89%80%e6%9c%89%e5%b1%9e%e6%80%a7%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25996\/","title":{"rendered":"\u5982\u4f55\u4ece HTML \u6807\u7b7e\u4e2d\u53bb\u9664\u6240\u6709\u5c5e\u6027\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173077219262589.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4ece HTML \u6807\u7b7e\u4e2d\u53bb\u9664\u6240\u6709\u5c5e\u6027\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4ece HTML \u6807\u7b7e\u4e2d\u53bb\u9664\u6240\u6709\u5c5e\u6027\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u6e05\u9664 html \u6807\u7b7e\u4e2d\u7684\u6240\u6709\u5c5e\u6027\uff1f<\/strong><\/p>\n<p>\u5f53\u4f60\u5c06 Word \u6587\u6863\u8f6c\u6362\u4e3a HTML \u65f6\uff0c\u53ef\u80fd\u4f1a\u5f97\u5230\u5305\u542b\u8bb8\u591a\u5c5e\u6027\u548c\u6837\u5f0f\u7684\u8868\u683c\u4ee3\u7801\u3002\u4e3a\u4e86\u83b7\u5f97\u57fa\u672c\u7684\u8868\u683c\u6807\u7b7e\u4ee3\u7801\uff0c\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570 removeAttributes\uff0c\u8be5\u51fd\u6570\u5c06\u63a5\u6536 HTML \u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<p>\u8be5\u51fd\u6570\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f pattern \u5339\u914d HTML \u6807\u7b7e\u53ca\u5176\u5c5e\u6027\u3002<\/p>\n<p>\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u5c06\u5339\u914d\u5230\u7684\u6807\u7b7e\u548c\u5c5e\u6027\u66ff\u6362\u4e3a\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre>function removeAttributes(htmlString) {\n  \/\/ \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914dHTML\u6807\u7b7e\u548c\u5c5e\u6027\n  var pattern = \/&lt;[^&gt;]+?(s+[^&gt;]*?)?&gt;\/gi;\n  \n  \/\/ \u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u5c06\u5339\u914d\u5230\u7684\u6807\u7b7e\u548c\u5c5e\u6027\u6e05\u9664\n  var cleanString = htmlString.replace(pattern, function (match) {\n    return match.replace(\/(s+w+(=[\"'][^\"']*[\"'])?)\/gi, '');\n  });\n  \n  return cleanString;\n}\n\n\/\/ \u793a\u4f8b\u7528\u6cd5\nvar htmlString = '&lt;p class=\"my-class\" style=\"color:red\"&gt;This is a paragraph.&lt;\/p&gt;';\nvar cleanedString = removeAttributes(htmlString);\nconsole.log(cleanedString); \/\/ &lt;p&gt;This is a paragraph.&lt;\/p&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u8c03\u7528 removeAttributes \u51fd\u6570\uff0c\u53ef\u4ee5\u6e05\u9664 HTML \u6807\u7b7e\u4e2d\u7684\u6240\u6709\u5c5e\u6027\uff0c\u53ea\u4fdd\u7559\u57fa\u672c\u6807\u7b7e\u4ee3\u7801\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4ece HTML \u6807\u7b7e\u4e2d\u53bb\u9664\u6240\u6709\u5c5e\u6027\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>\u5982\u4f55\u6e05\u9664 html \u6807\u7b7e\u4e2d\u7684\u6240\u6709\u5c5e\u6027\uff1f \u5f53\u4f60\u5c06 Word \u6587\u6863\u8f6c\u6362\u4e3a HTML \u65f6\uff0c\u53ef\u80fd\u4f1a\u5f97\u5230\u5305\u542b\u8bb8\u591a\u5c5e\u6027\u548c\u6837\u5f0f\u7684\u8868\u683c\u4ee3\u7801\u3002\u4e3a\u4e86\u83b7\u5f97\u57fa\u672c\u7684\u8868\u683c\u6807\u7b7e\u4ee3\u7801\uff0c\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570 removeAttributes\uff0c\u8be5\u51fd\u6570\u5c06\u63a5\u6536 HTML \u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u3002 \u8be5\u51fd\u6570\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f pattern \u5339\u914d HTML \u6807\u7b7e\u53ca\u5176\u5c5e\u6027\u3002 \u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u5c06\u5339\u914d\u5230\u7684\u6807\u7b7e\u548c\u5c5e\u6027\u66ff\u6362\u4e3a\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a function removeAttributes(htmlString) { \/\/ \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914dHTML\u6807\u7b7e\u548c\u5c5e\u6027 var pattern = \/&lt;[^&gt;]+?(s+[^&gt;]*?)?&gt;\/gi; \/\/ \u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u5c06\u5339\u914d\u5230\u7684\u6807\u7b7e\u548c\u5c5e\u6027\u6e05\u9664 var cleanString = htmlString.replace(pattern, function (match) { return match.replace(\/(s+w+(=[&#8220;&#8216;][^&#8221;&#8216;]*[&#8220;&#8216;])?)\/gi, &#8221;); }); return cleanString; } \/\/ \u793a\u4f8b\u7528\u6cd5 var htmlString = &#8216;&lt;p class=&#8221;my-class&#8221; style=&#8221;color:red&#8221;&gt;This is a [&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-25996","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25996","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=25996"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25996\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}