{"id":2464,"date":"2024-11-10T15:48:47","date_gmt":"2024-11-10T07:48:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2464\/"},"modified":"2024-11-10T15:48:47","modified_gmt":"2024-11-10T07:48:47","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-javascript-%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e6%b8%85%e9%99%a4-html-%e6%a0%87%e7%ad%be%e7%9a%84%e6%89%80%e6%9c%89%e5%b1%9e%e6%80%a7%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2464\/","title":{"rendered":"\u5982\u4f55\u7528 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u6e05\u9664 HTML \u6807\u7b7e\u7684\u6240\u6709\u5c5e\u6027\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173077092419369.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7528 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u6e05\u9664 HTML \u6807\u7b7e\u7684\u6240\u6709\u5c5e\u6027\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u7528 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u6e05\u9664 HTML \u6807\u7b7e\u7684\u6240\u6709\u5c5e\u6027\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u53bb\u9664 html \u6807\u7b7e\u6240\u6709\u5c5e\u6027<\/strong><\/p>\n<p>\u5728\u5904\u7406 HTML \u4ee3\u7801\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u53bb\u9664\u6807\u7b7e\u4e0a\u7684\u5c5e\u6027\uff0c\u4ee5\u4fbf\u83b7\u5f97\u66f4\u7b80\u6d01\u7684\u57fa\u7840\u4ee3\u7801\u3002\u672c\u6587\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f7f\u7528 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u6e05\u9664 HTML \u6807\u7b7e\u6240\u6709\u5c5e\u6027\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u8be5\u65b9\u6cd5\uff1a<\/p>\n<pre>function removeAttributes(htmlString) {\n  \/\/ \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d HTML \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 \u65b9\u6cd5\uff0c\u5373\u53ef\u53bb\u9664 HTML \u4ee3\u7801\u4e2d\u7684\u6240\u6709\u6807\u7b7e\u5c5e\u6027\uff0c\u751f\u6210\u66f4\u7b80\u6d01\u7684\u57fa\u7840\u4ee3\u7801\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u7528 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u6e05\u9664 HTML \u6807\u7b7e\u7684\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>\u53bb\u9664 html \u6807\u7b7e\u6240\u6709\u5c5e\u6027 \u5728\u5904\u7406 HTML \u4ee3\u7801\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u53bb\u9664\u6807\u7b7e\u4e0a\u7684\u5c5e\u6027\uff0c\u4ee5\u4fbf\u83b7\u5f97\u66f4\u7b80\u6d01\u7684\u57fa\u7840\u4ee3\u7801\u3002\u672c\u6587\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f7f\u7528 JavaScript \u6b63\u5219\u8868\u8fbe\u5f0f\u6e05\u9664 HTML \u6807\u7b7e\u6240\u6709\u5c5e\u6027\u7684\u65b9\u6cd5\u3002 \u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u8be5\u65b9\u6cd5\uff1a function removeAttributes(htmlString) { \/\/ \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d HTML \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+(=&#8221;[^&#8221;]*&#8221;)?)\/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); console.log(cleanedString); \/\/ &lt;p&gt;This [&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-2464","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2464","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=2464"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2464\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}