{"id":25972,"date":"2024-11-24T13:48:03","date_gmt":"2024-11-24T05:48:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25972\/"},"modified":"2024-11-24T13:48:03","modified_gmt":"2024-11-24T05:48:03","slug":"%e5%a6%82%e4%bd%95%e6%b8%85%e9%99%a4-html-%e6%a0%87%e7%ad%be%e4%b8%ad%e7%9a%84%e6%89%80%e6%9c%89%e5%b1%9e%e6%80%a7%ef%bc%8c%e5%b9%b6%e4%bf%9d%e7%95%99%e8%a1%a8%e6%a0%bc%e7%bb%93%e6%9e%84%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25972\/","title":{"rendered":"\u5982\u4f55\u6e05\u9664 HTML \u6807\u7b7e\u4e2d\u7684\u6240\u6709\u5c5e\u6027\uff0c\u5e76\u4fdd\u7559\u8868\u683c\u7ed3\u6784\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173076804377805.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u6e05\u9664 HTML \u6807\u7b7e\u4e2d\u7684\u6240\u6709\u5c5e\u6027\uff0c\u5e76\u4fdd\u7559\u8868\u683c\u7ed3\u6784\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u6e05\u9664 HTML \u6807\u7b7e\u4e2d\u7684\u6240\u6709\u5c5e\u6027\uff0c\u5e76\u4fdd\u7559\u8868\u683c\u7ed3\u6784\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>\u5982\u4f55\u5728\u4e0d\u4e22\u5931\u8868\u683c\u7ed3\u6784\u7684\u60c5\u51b5\u4e0b\u5220\u9664\u8f6c\u6362\u540e\u7684\u7f51\u9875\u4ee3\u7801\u4e2d\u7684\u8868\u683c\u5c5e\u6027\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8bc6\u522b\u548c\u6e05\u9664 HTML \u6807\u7b7e\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li> <strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff1a<\/strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f ]+?(s+[^&gt;]*?)?&gt; \u5339\u914d HTML \u6807\u7b7e\u548c\u5c5e\u6027\u3002<\/li>\n<li> <strong>\u5b57\u7b26\u4e32\u66ff\u6362\uff1a<\/strong>\u4f7f\u7528 String.replace() \u65b9\u6cd5\u5c06\u5339\u914d\u5230\u7684\u6807\u7b7e\u548c\u5c5e\u6027\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002<\/li>\n<li> <strong>\u9012\u5f52\u6e05\u9664\uff1a<\/strong>\u91cd\u590d\u6267\u884c\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u76f4\u5230\u6240\u6709\u5c5e\u6027\u90fd\u88ab\u6e05\u9664\u3002<\/li>\n<\/ol>\n<pre>function removeAttributes(htmlString) {\n  var pattern = \/&lt;[^&gt;]+?(s+[^&gt;]*?)?&gt;\/gi;\n  var cleanString = htmlString.replace(pattern, function (match) {\n    return match.replace(\/(s+w+(=[\"'][^\"']*[\"'])?)\/gi, '');\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>\u4f7f\u7528\u6b64\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4ece HTML \u4ee3\u7801\u4e2d\u6e05\u9664\u6240\u6709\u5c5e\u6027\uff0c\u53ea\u4fdd\u7559\u57fa\u7840\u6807\u7b7e\u7ed3\u6784\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u6e05\u9664 HTML \u6807\u7b7e\u4e2d\u7684\u6240\u6709\u5c5e\u6027\uff0c\u5e76\u4fdd\u7559\u8868\u683c\u7ed3\u6784\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 \u5982\u4f55\u5728\u4e0d\u4e22\u5931\u8868\u683c\u7ed3\u6784\u7684\u60c5\u51b5\u4e0b\u5220\u9664\u8f6c\u6362\u540e\u7684\u7f51\u9875\u4ee3\u7801\u4e2d\u7684\u8868\u683c\u5c5e\u6027\uff1f \u89e3\u51b3\u65b9\u6848\uff1a \u53ef\u4ee5\u4f7f\u7528 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8bc6\u522b\u548c\u6e05\u9664 HTML \u6807\u7b7e\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f ]+?(s+[^&gt;]*?)?&gt; \u5339\u914d HTML \u6807\u7b7e\u548c\u5c5e\u6027\u3002 \u5b57\u7b26\u4e32\u66ff\u6362\uff1a\u4f7f\u7528 String.replace() \u65b9\u6cd5\u5c06\u5339\u914d\u5230\u7684\u6807\u7b7e\u548c\u5c5e\u6027\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002 \u9012\u5f52\u6e05\u9664\uff1a\u91cd\u590d\u6267\u884c\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u76f4\u5230\u6240\u6709\u5c5e\u6027\u90fd\u88ab\u6e05\u9664\u3002 function removeAttributes(htmlString) { var pattern = \/&lt;[^&gt;]+?(s+[^&gt;]*?)?&gt;\/gi; 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 paragraph.&lt;\/p&gt;&#8217;; var cleanedString = removeAttributes(htmlString); [&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-25972","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25972","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=25972"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25972\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}