{"id":9770,"date":"2021-01-21T00:21:55","date_gmt":"2021-01-20T16:21:55","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9770"},"modified":"2021-01-21T00:21:55","modified_gmt":"2021-01-20T16:21:55","slug":"dom-api-html-encode-decode","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/01\/dom-api-html-encode-decode\/","title":{"rendered":"\u5de7\u7528DOM API\u5b9e\u73b0HTML\u5b57\u7b26\u7684\u8f6c\u4e49\u548c\u53cd\u8f6c\u4e49"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9770\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9770<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202101-dom-api-encode.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/html-code-cover.png\" width=\"480\" height=\"318\" alt=\"DOM API \u8f6c\u4e49\u548c\u53cd\u8f6c\u4e49HTML\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001HTML\u5b57\u7b26\u7684\u8f6c\u4e49<\/h3>\n<p>\u4e0d\u8f6c\u5f2f\uff0cHTML\u5b57\u7b26\u8f6c\u4e49\u6709\u66f4\u7b80\u5355\u66f4\u5bb9\u6613\u8bb0\u5fc6\u7684\u65b9\u6cd5\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>let textNode = document.createTextNode('&lt;span&gt;by zhangxinxu&lt;\/span&gt;');\r\nlet div = document.createElement('div');\r\ndiv.append(textNode);\r\nconsole.log(div.innerHTML);<\/pre>\n<p>\u4e5f\u5c31\u662f\u628aHTML\u5185\u5bb9\u4f5c\u4e3a\u6587\u672c\u8282\u70b9\u7684textContent\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528\u666e\u901a\u5143\u7d20\u7684innerHTML\u5c5e\u6027\u8fd4\u56de\u4e0b\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u4e0a\u9762\u4ee3\u7801\u8f93\u51fa\u7684\u7ed3\u679c\u662f\uff1a<\/p>\n<pre>&amp;lt;span&amp;gt;by zhangxinxu&amp;lt;\/span&amp;gt;<\/pre>\n<p>\u5927\u5bb6\u53ef\u4ee5\u590d\u5236\u4e0a\u9762\u4ee3\u7801\u5728\u63a7\u5236\u53f0\u8dd1\u4e00\u4e0b\uff0c\u4f8b\u5982\u4e0b\u56fe\u5c31\u662f\u6211\u5728Chrome\u6d4f\u89c8\u5668\u4e2d\u8fd0\u884c\u7684\u7ed3\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/2021-01-20_234716.png\" width=\"512\" height=\"163\" alt=\"DOM API\u5b9e\u73b0HTML\u8f6c\u4e49\u793a\u610f\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e8c\u3001HTML\u5b57\u7b26\u7684\u53cd\u8f6c\u4e49<\/h3>\n<p>\u8fd9\u4e2a\u9700\u8981\u7528\u5230\u7684DOM API\u5c31\u76f8\u5bf9\u7a00\u7f55\u4e86\u4e00\u70b9\uff0c\u4f7f\u7528DOMParser API\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre>let str = '&amp;lt;span&amp;gt;by zhangxinxu&amp;lt;\/span&amp;gt;';\r\nlet doc = new DOMParser().parseFromString(str, 'text\/html');\r\nconsole.log(doc.documentElement.textContent);<\/pre>\n<p>\u7ed3\u679c\u5c31\u662f\uff1a<\/p>\n<pre>&lt;span&gt;by zhangxinxu&lt;\/span&gt;<\/pre>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u8fd0\u884c\u622a\u56fe\u53c2\u89c1\u4e0b\u65b9\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/2021-01-20_235625.png\" width=\"465\" height=\"157\" alt=\"HTML\u53cd\u8f6c\u4e49\u65b9\u6cd5\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<hr>\n<p>\u7136\u540e\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u662f\u501f\u52a9<code>&lt;textarea&gt;<\/code>\u5143\u7d20\uff0c\u8fd9\u662fIE\u6d4f\u89c8\u5668\u65f6\u4ee3\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/p>\n<pre>let textarea = document.createElement('textarea');\r\ntextarea.innerHTML = '&amp;lt;span&amp;gt;by zhangxinxu&amp;lt;\/span&amp;gt;';\r\nconsole.log(textarea.childNodes[0].nodeValue);<\/pre>\n<p>\u7ed3\u679c\u4e5f\u662f\u4e00\u6837\u7684\uff0c\u8f6c\u4e49\u7684HTML\u6807\u7b7e\u90fd\u53cd\u8f6c\u4e49\u56de\u6765\u4e86\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/2021-01-21_000235.png\" width=\"482\" height=\"139\" alt=\"textarea\u5143\u7d20\u53cd\u8f6c\u4e49HTML\" class=\"alignnone size-medium\" \/><\/p>\n<p><span class=\"hidden\">\/\/zxx: \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\uff0c\u8bf4\u660e\u4f60\u73b0\u5728\u8bbf\u95ee\u662f\u4f53\u9a8c\u7cdf\u7cd5\u7684\u5783\u573e\u76d7\u7248\u7f51\u7ad9\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u539f\u6587\u83b7\u5f97\u5f88\u597d\u7684\u4f53\u9a8c\uff1ahttps:\/\/www.zhangxinxu.com\/wordpress\/?p=9770\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<h3>\u4e09\u3001DOM API\u65b9\u6cd5\u7684\u7f3a\u70b9<\/h3>\n<p>DOM API\u65b9\u6cd5\u5229\u7528\u4e86\u6d4f\u89c8\u5668\u7684\u80fd\u529b\uff0c\u66f4\u5bb9\u6613\u4e0a\u624b\uff0c\u8f6c\u4e49\u7ed3\u679c\u4e5f\u66f4\u5b89\u5168\uff0c\u4f46\u662f\u6709\u4e2a\u4e0d\u8db3\uff0c\u90a3\u5c31\u662f\u53ea\u80fd\u5728\u6d4f\u89c8\u5668\u4e0a\u4e0b\u6587\u73af\u5883\u4e2d\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5982\u679c\u662fService Workers\u73af\u5883\uff0c\u6216\u8005\u662fNode.js\u73af\u5883\u4e2d\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u4e0d\u884c\u4e86\uff0c\u53ea\u80fd\u4f7f\u7528\u4f20\u7edf\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u4e86\u3002<\/p>\n<p>\u4f20\u7edf\u7684\u5b57\u7b26\u4e32\u5904\u7406\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre><span style=\"color:green;\">\/**\r\n * \u8f6c\u4e49HTML\u6807\u7b7e\u7684\u65b9\u6cd5\r\n * @param  {String} str \u9700\u8981\u8f6c\u4e49\u7684HTML\u5b57\u7b26\u4e32\r\n * @return {String}     \u8f6c\u4e49\u540e\u7684\u5b57\u7b26\u4e32\r\n *\/<\/span>\r\nvar funEncodeHTML = function (str) {\r\n    if (typeof str == 'string') {\r\n        return str.replace(\/&lt;|&amp;|&gt;\/g, function (matches) {\r\n            return ({\r\n                '&lt;': '&amp;lt;',\r\n                '&gt;': '&amp;gt;',\r\n                '&amp;': '&amp;amp;'\r\n            })[matches];\r\n        });\r\n    }\r\n\r\n    return '';\r\n};<\/pre>\n<pre><span style=\"color:green;\">\/**\r\n * \u53cd\u8f6c\u4e49HTML\u6807\u7b7e\u7684\u65b9\u6cd5\r\n * @param  {String} str \u9700\u8981\u53cd\u8f6c\u4e49\u7684\u5b57\u7b26\u4e32\r\n * @return {String}     \u53cd\u8f6c\u4e49\u540e\u7684\u5b57\u7b26\u4e32\r\n *\/<\/span>\r\nvar funDecodeHTML = function (str) {\r\n    if (typeof str == 'string') {\r\n        return str.replace(\/&amp;lt;|&amp;gt;|&amp;amp;\/g, function (matches) {\r\n            return ({\r\n                '&amp;lt;': '&lt;',\r\n                '&amp;gt;': '&gt;',\r\n                '&amp;amp;': '&amp;'\r\n            })[matches];\r\n        });\r\n    }\r\n\r\n    return '';\r\n};<\/pre>\n<h3>\u56db\u3001\u723d\u8a00\u723d\u8bed<\/h3>\n<p>\u6700\u8fd1\u4e00\u5468\u5728\u66f4\u6362\u7f51\u7ad9\u5907\u6848\uff0c\u76ee\u524d\u5df2\u7ecf\u5907\u6848\u597d\u4e86\uff0c\u7f51\u7ad9\u8bbf\u95ee\u5c06\u4f1a\u6062\u590d\u6b63\u5e38\uff0c\u6587\u7ae0\u66f4\u65b0\u4e5f\u4f1a\u56de\u5230\u6b63\u5e38\u9891\u7387\u3002<\/p>\n<p>OK\uff0c\u8fd9\u5c31\u662f\u672c\u6587\u5168\u90e8\u5185\u5bb9\u5566\uff01<\/p>\n<p>\u611f\u8c22\u5927\u5bb6\u652f\u6301\uff0c\u5982\u679c\u89c9\u5f97\u76f8\u5173\u77e5\u8bc6\u8fd8\u771f\u6709\u7528\uff0c\u6b22\u8fce\u8f6c\u53d1\u5230\u670b\u53cb\u5708\uff0c\u5982\u679c\u662fPC\u7aef\u8bbf\u95ee\uff0c\u5219\u5de6\u4fa7\u6216\u4e0b\u65b9\u5e94\u8be5\u53ef\u4ee5\u770b\u5230\u5fae\u4fe1\u56fe\u6807\u6309\u94ae\uff0c\u70b9\u51fb\u3001\u626b\u7801\u5206\u4eab\uff0c\u611f\u8c22\u611f\u8c22\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/2764.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9770\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9770<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202101\/html-code-cover.png\" width=\"160\" height=\"106\" alt=\"DOM API \u8f6c\u4e49\u548c\u53cd\u8f6c\u4e49HTML\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u501f\u52a9DOM API\u7684\u81ea\u8eab\u7279\u6027\u8f6c\u4e49\u6216\u8005\u53cd\u8f6c\u4e49HTML\uff0c\u4f60\u4f1a\u53d1\u73b0\u539f\u6765\u8fd9\u4e48\u7b80\u5355\uff0c\u8fd9\u4e48\u8f7b\u677e\uff0c\u672c\u6587\u5185\u5bb9\u77ed\u5c0f\u7cbe\u608d\uff0c\u53ef\u8c13Web\u524d\u7aef\u5f00\u53d1\u5fc5\u5b66\u5fc5\u4f1a\u57fa\u672c\u6280\u5de7\u4e4b\u4e00\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[1702,1701,1296,1441,1295,552,1443,515,1484],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9770"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=9770"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9770\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}