{"id":9835,"date":"2021-02-16T17:36:43","date_gmt":"2021-02-16T09:36:43","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9835"},"modified":"2021-02-16T17:36:43","modified_gmt":"2021-02-16T09:36:43","slug":"html-string-dom","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/02\/html-string-dom\/","title":{"rendered":"\u76d8\u70b9HTML\u5b57\u7b26\u4e32\u8f6cDOM\u7684\u5404\u79cd\u65b9\u6cd5\u53ca\u7ec6\u8282"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9835\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9835<\/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<style>.single-h{letter-spacing:-1px;}<\/style>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202102-html2dom.jpg';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202102\/html-to-dom-cover.jpg\" width=\"480\" height=\"301\" alt=\"\u65b0\u5e74\u65f6\u949fDOM HTML\u5b57\u7b26\u4e32\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<p>HTML\u5b57\u7b26\u4e32\u8f6cDOM\u65b9\u6cd5\u6709\u597d\u51e0\u4e2a\uff0c\u8fd9\u91cc\u76d8\u70b9\u4e0b\u5404\u4e2a\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u4ee5\u53ca\u5173\u4e8eDOM\u7684\u5176\u4ed6\u4e00\u4e9b\u7ec6\u8282\u3002<\/p>\n<h3>\u4e00\u3001\u65b9\u6cd5\u76d8\u70b9<\/h3>\n<h4>1. innerHTML<\/h4>\n<pre>let html = '\u6587\u672c&lt;span&gt;\u5143\u7d20&lt;\/span&gt;';\r\nlet placeholder = document.createElement('div');\r\nplaceholder.innerHTML = html;\r\nlet nodes = placeholder.childNodes;<\/pre>\n<p>\u5176\u4e2d<code>nodes<\/code>\u5305\u542b\u4e86\u6240\u6709\u7684\u8282\u70b9\u4fe1\u606f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202102\/2021-02-16_124929.png\" width=\"377\" height=\"201\" alt=\"\u8282\u70b9\u4fe1\u606f\" class=\"alignnone size-medium\" \/><\/p>\n<table border=\"0\" width=\"100%\" class=\"params_table\" cellspacing=\"1\">\n<tbody>\n<tr>\n<th>\u5b89\u5168\u6027<\/th>\n<td><span style=\"color:green;\">\u2714<\/span><\/td>\n<td>script\u811a\u672c\u4e0d\u4f1a\u6267\u884c<\/td>\n<\/tr>\n<tr>\n<th>\u4efb\u610f\u8282\u70b9<\/th>\n<td><span style=\"color:red;\">\u2718<\/span><\/td>\n<td>\u4e0d\u5408\u6cd5\u7684\u8282\u70b9\u65e0\u6cd5\u8f6c\u6362\uff0c\u4f8b\u5982p\u6807\u7b7e\u91cc\u9762\u5d4c\u5957div\u3002\u9664\u975e\u4f7f\u7528<code>&lt;template&gt;<\/code>\u5143\u7d20\uff0c\u672c\u6587\u540e\u9762\u4f1a\u6709\u4ecb\u7ecd\u3002<\/td>\n<\/tr>\n<tr>\n<th>\u517c\u5bb9\u6027<\/th>\n<td><span style=\"color:orange;\">\u2605\u2605\u2605\u2605<\/span><\/td>\n<td>IE6+\u5747\u652f\u6301<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2. insertAdjacentHTML<\/h4>\n<pre>let html = '&lt;span&gt;\u5143\u7d20&lt;\/span&gt;';\r\nlet placeholder = document.createElement('div');\r\nplaceholder.insertAdjacentHTML('afterbegin', html);\r\n<span style=\"color:green;\">\/\/ \u5982\u679cHTML\u662f\u5355\u4e2a\u95ed\u5408HTML\u6807\u7b7e\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528firstElementChild\u76f4\u63a5\u8fd4\u56de<\/span>\r\nlet node = placeholder.firstElementChild;<\/pre>\n<p><code>insertAdjacentHTML<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6307\u5b9aHTML\u5b57\u7b26\u63d2\u5165\u7684\u4f4d\u7f6e\uff0c\u652f\u63014\u4e2a\u65b9\u4f4d\uff0c\u7279\u6027\u76d8\u70b9\u53c2\u89c1\u4e0b\u8868\uff1a<\/p>\n<table border=\"0\" width=\"100%\" class=\"params_table\" cellspacing=\"1\">\n<tbody>\n<tr>\n<th>\u5b89\u5168\u6027<\/th>\n<td><span style=\"color:green;\">\u2714<\/span><\/td>\n<td>script\u811a\u672c\u4e0d\u4f1a\u6267\u884c<\/td>\n<\/tr>\n<tr>\n<th>\u4efb\u610f\u8282\u70b9<\/th>\n<td><span style=\"color:red;\">\u2718<\/span><\/td>\n<td>\u4e0d\u5408\u6cd5\u7684\u8282\u70b9\u65e0\u6cd5\u8f6c\u6362\uff0c\u4f8b\u5982div\u6807\u7b7e\u91cc\u9762\u5d4c\u5957tr<\/td>\n<\/tr>\n<tr>\n<th>\u517c\u5bb9\u6027<\/th>\n<td><span style=\"color:orange;\">\u2605\u2605\u2605<\/span><\/td>\n<td>IE6+\u652f\u6301\uff0cIE10-\u4e0d\u652f\u6301table\u76f8\u5173\u7684\u6807\u7b7e\uff0c\u4f8b\u5982tr, td, thead\u7b49\u5143\u7d20\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>3. DOMParser<\/h4>\n<p>\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>nodes = new DOMParser().parseFromString(html, 'text\/html').body.childNodes;<\/pre>\n<p><code>DOMParser()<\/code>\u53ef\u4ee5\u89e3\u6790HTML\u5b57\u7b26\u4e32\uff0c\u4e0d\u4ec5\u666e\u901aHTML\u5b57\u7b26\u4e32\uff0c\u4f20\u7edf\u7684XML\u5b57\u7b26\u4e32\uff0cSVG\u5b57\u7b26\u4e32\u90fd\u53ef\u4ee5\u89e3\u6790\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>DOMParser()<\/code>\u65b9\u6cd5\u53cd\u8f6c\u4e49HTML\u6807\u7b7e\uff0c\u8be6\u89c1\u8fd9\u7bc7\u6587\u7ae0\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/01\/dom-api-html-encode-decode\/\">\u5de7\u7528DOM API\u5b9e\u73b0HTML\u5b57\u7b26\u7684\u8f6c\u4e49\u548c\u53cd\u8f6c\u4e49<\/a>\u201d\u3002<\/p>\n<p>DOMParser\u7684\u76f8\u5173\u7279\u6027\u5982\u4e0b\u8868\u6240\u793a\uff1a<\/p>\n<table border=\"0\" width=\"100%\" class=\"params_table\" cellspacing=\"1\">\n<tbody>\n<tr>\n<th>\u5b89\u5168\u6027<\/th>\n<td><span style=\"color:green;\">\u2714<\/span><\/td>\n<td>script\u811a\u672c\u4e0d\u4f1a\u6267\u884c<\/td>\n<\/tr>\n<tr>\n<th>\u4efb\u610f\u8282\u70b9<\/th>\n<td><span style=\"color:red;\">\u2718<\/span><\/td>\n<td>\u53ef\u4ee5\u4f5c\u4e3abody\u5b50\u5143\u7d20\u7684\u6807\u7b7e\u624d\u53ef\u4ee5\u89e3\u6790\uff0c\u4f8b\u5982 <code>'&lt;tr&gt;text&lt;\/tr&gt;'<\/code> \u5c31\u65e0\u6cd5\u89e3\u6790\u51fa<code>&lt;tr&gt;<\/code>\u5143\u7d20\u3002<\/td>\n<\/tr>\n<tr>\n<th>\u517c\u5bb9\u6027<\/th>\n<td><span style=\"color:orange;\">\u2605\u2605<\/span><\/td>\n<td>IE 10+, Safari 9.1+<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>4. Range<\/h4>\n<p>\u6b64\u65b9\u6cd5\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>elements = document.createRange().createContextualFragment(html).children;<\/pre>\n<p>\u8fd9\u91cc\u4f7f\u7528\u4e86chidlren\u5c5e\u6027\u793a\u610f\uff0c\u53ef\u4ee5\u8fd4\u56deHTMLCollection\uff0c\u5143\u7d20\u5408\u96c6\uff0c\u5ffd\u7565\u6587\u672c\u3002<\/p>\n<p>\u7279\u6027\u5982\u4e0b\uff1a<\/p>\n<table border=\"0\" width=\"100%\" class=\"params_table\" cellspacing=\"1\">\n<tbody>\n<tr>\n<th>\u5b89\u5168\u6027<\/th>\n<td><span style=\"color:green;\">\u2718<\/span><\/td>\n<td>script\u811a\u672c\u4f1a\u6267\u884c\uff0c\u4f7f\u7528\u65f6\u5019\u9700\u8981\u6ce8\u610f<\/td>\n<\/tr>\n<tr>\n<th>\u4efb\u610f\u8282\u70b9<\/th>\n<td><span style=\"color:red;\">\u2718<\/span><\/td>\n<td>\u6b64API\u53ef\u4ee5\u6307\u5b9a\u5408\u9002\u7684\u4e0a\u4e0b\u6587\uff0c\u6b64\u65f6\u624d\u80fd\u65b9\u4fbf\u89e3\u6790\u3002<\/td>\n<\/tr>\n<tr>\n<th>\u517c\u5bb9\u6027<\/th>\n<td><span style=\"color:orange;\">\u2605\u2605<\/span><\/td>\n<td>IE 10+, Safari 9+<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\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=9835\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<h3>\u4e8c\u3001DOM\u8f6c\u6362\u5408\u6cd5\u6027\u7684\u5904\u7406<\/h3>\n<p>\u4e0a\u9762\u7684DOM\u8f6c\u6362\u65b9\u6cd5\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u7279\u6b8a\u7684\u6807\u7b7e\uff0c\u5c24\u5176\u662f\u8868\u683c\u76f8\u5173\u7684\u5143\u7d20\uff0c\u76f4\u63a5\u4f7f\u7528\u53ef\u80fd\u662f\u65e0\u6548\u7684\u3002<\/p>\n<p>\u4f8b\u5982\u4e0b\u9762\u7684\uff1a<\/p>\n<pre>let placeholder = document.createElement('div');\r\nplaceholder.innerHTML = `&lt;tr&gt;&lt;td&gt;zhangxinxu.com&lt;\/td&gt;&lt;\/tr&gt;`;\r\nconsole.log(placeholder.firstElementChild);    <span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f null<\/span><\/pre>\n<p>div\u5143\u7d20\u4e2d\u63d2\u5165tr\u6807\u7b7e\u76f8\u5173\u7684HTML\u5b57\u7b26\u4e32\uff0c\u8fd9\u6bb5HTML\u4f1a\u8ba4\u4e3a\u662f\u4e0d\u5408\u6cd5\u7684\uff0c\u4ece\u800c\u4e0d\u4f1a\u8f6c\u6362\u4e3atr\u5143\u7d20\u3002<\/p>\n<p>\u6b64\u65f6\u9700\u8981\u901a\u8fc7\u521b\u5efa<code>table<\/code>\u5143\u7d20\u6765\u8fd4\u56de\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>let placeholder = document.createElement('tbody');\r\nplaceholder.innerHTML = `&lt;tr&gt;&lt;td&gt;zhangxinxu.com&lt;\/td&gt;&lt;\/tr&gt;`;\r\nconsole.log(placeholder.firstElementChild);    <span style=\"color:green;\">\/\/ \u7ed3\u679c\u662f tr \u5143\u7d20<\/span><\/pre>\n<p>\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u8fd0\u884c\u6548\u679c\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202102\/2021-02-16_162856.png\" width=\"436\" height=\"165\" alt=\"tr\u5728tbody\u4e0b\u6267\u884c\u6548\u679c\" class=\"alignnone size-medium\" \/><\/p>\n<h4>createContextualFragment<\/h4>\n<p><code>createContextualFragment<\/code>\u65b9\u6cd5\u4e5f\u662f\u7c7b\u4f3c\u7684\u5904\u7406\uff0c\u9700\u8981\u6307\u5b9a<code>tbody<\/code>\u5143\u7d20\u4f5c\u4e3aRange\u533a\u57df\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre>let tbody = document.createElement(`tbody`);\r\nlet range = document.createRange();\r\nrange.selectNodeContents(tbody);\r\nconsole.log(range.createContextualFragment(`&lt;tr&gt;&lt;td&gt;zhangxinxu&lt;\/td&gt;&lt;\/tr&gt;`).firstChild);<\/pre>\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202102\/2021-02-16_165350.png\" width=\"485\" height=\"196\" alt=\"Range\u65b9\u6cd5tr\u5143\u7d20\u8fd0\u884c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<h4>template<\/h4>\n<p>\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u4e0d\u9700\u8981\u8003\u8651\u4e0a\u4e0b\u6587\uff0c\u5373\u53ef\u5b9e\u73b0\u4efb\u610f\u6807\u7b7eHTML\u5143\u7d20\u7684DOM\u8f6c\u6362\uff0c\u8fd9\u4e2a\u5143\u7d20\u5c31\u662fHTML5 <code>&lt;template&gt;<\/code>\u5143\u7d20\u3002<\/p>\n<p>JavaScript\u4ee3\u7801\u4f7f\u7528\u793a\u610f\uff1a<\/p>\n<pre>let template = document.createElement('template');\r\ntemplate.innerHTML = `&lt;tr&gt;&lt;td&gt;\u7981\u6b62\u516c\u4f17\u53f7\u8f6c\u8f7d&lt;\/td&gt;&lt;\/tr&gt;`;\r\nconsole.log(template.content.lastChild);<\/pre>\n<p>\u540c\u6837\u53ef\u4ee5\u8fd4\u56de<code>&lt;tr&gt;<\/code>\u5143\u7d20\uff0c\u4f8b\u5982\u5728Chrome console\u63a7\u5236\u53f0\u8fd0\u884c\u7684\u7ed3\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202102\/2021-02-16_165816.png\" width=\"422\" height=\"173\" alt=\"template\u5143\u7d20\u7684\u8f6c\u6362\" class=\"alignnone size-medium\" \/><\/p>\n<p><code>&lt;template&gt;<\/code>\u5143\u7d20\u867d\u7136\u597d\u4f7f\uff0c\u4f46\u662f\u517c\u5bb9\u6027\u4e0d\u8db3\uff0cIE\u6d4f\u89c8\u5668\u4e0d\u652f\u6301\uff0c\u5173\u4e8e\u8be5\u5143\u7d20\u66f4\u591a\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003\u6211\u591a\u5e74\u524d\u5199\u7684\u8fd9\u7bc7\u6587\u7ae0\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2014\/07\/hello-html5-template-tag\/\" rel=\"noopener\" target=\"_blank\">HTML5 &lt;template&gt;\u6807\u7b7e\u5143\u7d20\u7b80\u4ecb<\/a>\u201d\u3002<\/p>\n<h3>\u4e09\u3001\u5173\u4e8eHTML\u5b57\u7b26\u4e32\u4e2dJS\u7684\u6267\u884c<\/h3>\n<p><code>createContextualFragment<\/code>\u65b9\u6cd5\u4f1a\u6267\u884c\u5185\u8054\u7684script js\u4ee3\u7801\uff0c\u4e3a\u4e86\u5b89\u5168\u8d77\u89c1\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c3d\u91cf\u4e0d\u8981\u4f7f\u7528\uff0c\u907f\u514d\u88ab\u653b\u51fb\uff0c\u9664\u975e\u5f53\u524d\u7684\u9700\u6c42\u5c31\u662f\u5e0c\u671b\u91cc\u9762\u7684JS\u4ee3\u7801\u4e00\u8d77\u652f\u6301\u3002<\/p>\n<p>\u800c\u5176\u4ed6\u7684innerHTML\u65b9\u6cd5\u3001\u6216\u8005insertAdjacentHTML\u65b9\u6cd5\u90fd\u4e0d\u7528\u62c5\u5fc3JS\u4f1a\u88ab\u652f\u6301\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>let placeholder = document.createElement('div');\r\nplaceholder.innerHTML = `&lt;div&gt;&lt;script&gt;alert('\u946b\u7a7a\u95f4');&lt;\/script&gt;&lt;\/div&gt;`;\r\nlet node = placeholder.firstElementChild;\r\n\r\ndocument.body.appendChild(node); <span style=\"color:green;\">\/\/ \u4e0d\u4f1a\u6709alert\u63d0\u793a<\/span><\/pre>\n<p>\u4f46\u662f\uff0c\u4f7f\u7528HTML\u5c5e\u6027\u8bbe\u7f6e\u7684JS\u4ee3\u7801\u662f\u4f1a\u8fd0\u884c\u7684\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>let placeholder = document.createElement('div');\r\nplaceholder.innerHTML = `&lt;img src='x' <span style=\"color:#cd0000;\">onerror='alert(1)'<\/span>&gt;`;\r\nlet node = placeholder.firstElementChild;\r\n\r\ndocument.body.appendChild(node);<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5728Chrome\u6d4f\u89c8\u5668\u4e0b\u5c31\u4f1a\u6267\u884c<code>alert(1)<\/code>\uff0c\u5176\u4ed6\u7c7b\u4f3c\u7684\u5c5e\u6027\u8fd8\u6709 <code>onload<\/code>\uff0c\u4f8b\u5982\u4e0a\u9762\u4ee3\u7801onerror\u6362\u6210onload\uff0csrc\u6539\u6210\u6b63\u5e38\u7684URL\u5730\u5740\uff0c\u5728Chrome\u63a7\u5236\u53f0\u8dd1\u4e00\u4e0b\uff0c\u5c31\u4f1a\u770b\u5230\u4ee3\u7801\u6267\u884c\u4e86\u3002<\/p>\n<p>\u6211\u81ea\u5df1\u5c31\u6d4b\u4e86\u4e0b\uff0c\u622a\u56fe\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202102\/2021-02-16_170854.png\" width=\"787\" height=\"296\" alt=\"onload\u6267\u884c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u56e0\u6b64\uff0c\u5728\u8fdb\u884cHTML\u5b57\u7b26\u4e32\u8f6cDOM\u65f6\u5019\uff0c\u8bb0\u5f97\u8fc7\u6ee4onload\u548conerror\u5c5e\u6027\uff0c\u5c24\u5176\u662f\u7b2c3\u65b9\u7684HTML\u5b57\u7b26\u4e32\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u5b89\u5168\uff0c\u9632\u6b62XSS\u653b\u51fb\u3002<\/p>\n<h3>\u56db\u3001\u5173\u4e8e\u8f6c\u6362\u7684\u6027\u80fd<\/h3>\n<p>\u6d4b\u8bd5\u6570\u636e\u6e90\u81ea\u8fd9\u91cc\uff1a<a href=\"https:\/\/jsperf.com\/html-string-to-dom-node\" rel=\"noopener nofollow\" target=\"_blank\">jsPerf benchmark<\/a>\uff0c\u4e0b\u9762\u662f\u7ed3\u679c\uff1a<\/p>\n<ol>\n<li><code>Range.createContextualFragment()<\/code> \u2014 <strong>winner<\/strong> (Firefox\u6d4f\u89c8\u5668\u4e2d\u6700\u5feb)<\/li>\n<li><code>Element.insertAdjacentHTML()<\/code> \u2014&nbsp;<strong>winner<\/strong><\/li>\n<li><code>Element.innerHTML<\/code> \u2014 <strong>winner<\/strong><\/li>\n<li><code>DOMParser.parseFromString()<\/code> \u2014 <strong>90% slower<\/strong><\/li>\n<\/ol>\n<p>\u7ed3\u8bba\u662fDOMParser\u65b9\u6cd5\u6027\u80fd\u6700\u5dee\u3002<\/p>\n<p>\u4e0d\u8fc7\u4e0a\u9762\u7684\u6d4b\u8bd5\u7ed3\u679c\u5bf9\u6211\u4eec\u65e5\u5e38\u5f00\u53d1\u5f71\u54cd\u4e0d\u5927\uff0c\u56e0\u4e3a\u901a\u5e38\u7684HTML\u89e3\u6790\u90fd\u662f\u5f88\u8f7b\u91cf\u7684\uff0c\u6027\u80fd\u8fd9\u5757\u6ca1\u6709\u672c\u8d28\u533a\u522b\u3002\u9664\u975e\u662f\uff0c\u90a3\u79cd\u51e0\u767eK\u7684HTML\u6216\u8005\u4e0aM\u5146\u7684SVG\u6587\u4ef6\u7684\u89e3\u6790\u3002<\/p>\n<h3>\u4e94\u3001\u603b\u7ed3\u548c\u7ed3\u8bed<\/h3>\n<p>innerHTML\u65b9\u6cd5\u6700\u6734\u5b9e\u65e0\u534e\uff0c\u6027\u4ef7\u6bd4\u6700\u9ad8\uff0c\u5982\u679c\u4e0d\u8003\u8651IE\u6d4f\u89c8\u5668\uff0c\u76f4\u63a5\u4f7f\u7528template\u5143\u7d20\u518dinnerHTML\uff0c\u4efb\u610fHTML\u6807\u7b7e\u5143\u7d20\u90fd\u53ef\u4ee5\u8f6c\u6362\u6210DOM\u3002<\/p>\n<p>\u6ce8\u610f\u5b89\u5168\uff0c\u9632\u6b62XSS\u653b\u51fb\u3002<\/p>\n<p>DOMParser\u65b9\u6cd5\u6027\u80fd\u7a0d\u5dee\uff0c\u4e0d\u8fc7\u975e\u5927\u89c4\u6a21\u5e94\u7528\u573a\u666f\u4e0d\u80fd\u4f5c\u4e3a\u6280\u672f\u9009\u578b\u7684\u8003\u91cf\u56e0\u7d20\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u6b22\u8fce\u8f6c\u53d1\u3001\u6b22\u8fce\u5206\u4eab\u3001\u4e5f\u6b22\u8fce\u8865\u5145\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6bd4\u5fc3\u611f\u8c22\u3002<\/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<h4>\u53c2\u8003\u6587\u7ae0<\/h4>\n<p>\u672c\u6587\u53c2\u8003\u81ea\uff1a<a href=\"https:\/\/grrr.tech\/posts\/create-dom-node-from-html-string\/\" rel=\"noopener nofollow\" target=\"_blank\">Create a DOM node from an HTML string<\/a> <\/p>\n<p>\u4fee\u6b63\u4e86\u539f\u6587\u90e8\u5206\u9519\u8bef\uff0c\u8865\u5145\u4e86\u82e5\u5e72\u622a\u56fe\u4fe1\u606f\u3002<\/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=9835\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9835<\/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\/202102\/html-to-dom-cover.jpg\" width=\"160\" height=\"100\" alt=\"\u65b0\u5e74\u65f6\u949fDOM HTML\u5b57\u7b26\u4e32\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>HTML\u5b57\u7b26\u4e32\u8f6cDOM\u65b9\u6cd5\u6709\u597d\u51e0\u4e2a\uff0c\u672c\u6587\u76d8\u70b9\u4e0b\u5404\u4e2a\u65b9\u6cd5\u7684\u7279\u6027\u548c\u4f18\u7f3a\u70b9\uff0c\u4ee5\u53ca\u60a8\u53ef\u80fd\u4e0d\u77e5\u9053\u7684\u76f8\u5173DOM API\u65b9\u6cd5\u7684\u5176\u4ed6\u7ec6\u8282\uff0c\u5e0c\u671b\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\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":[1727,1728,1725,1726,182,1441,809,1724,552,813,1443,928],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9835"}],"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=9835"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9835\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}