{"id":9607,"date":"2020-10-22T01:29:26","date_gmt":"2020-10-21T17:29:26","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9607"},"modified":"2020-11-17T17:45:31","modified_gmt":"2020-11-17T09:45:31","slug":"es6-html-template-literal","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2020\/10\/es6-html-template-literal\/","title":{"rendered":"ES6\u6a21\u677f\u5b57\u7b26\u4e32\u5728HTML\u6a21\u677f\u6e32\u67d3\u4e2d\u7684\u5e94\u7528"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9607\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9607<\/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\/202010-es6-template-html.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/es6-html-template-cover.png\" width=\"480\" height=\"310\" alt=\"\u6a21\u677f\u5b57\u9762\u91cfHTML\u6e32\u67d3\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u5148\u4e86\u89e3\u4e0bES6\u6a21\u677f\u5b57\u7b26\u4e32<\/h3>\n<p>\u8fc7\u53bb\u62fc\u63a5\u5b57\u7b26\u4e32\u591a\u91c7\u7528\u52a0\u53f7\u7684\u5f62\u5f0f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>var data = {\r\n    username: 'zhangxinxu'\r\n};\r\nconsole.log('\u672c\u6587\u4f5c\u8005\u662f\uff1a' + data.username);<\/pre>\n<p>\u6709\u4e24\u4e2a\u4e0d\u8db3\uff0c\u4e00\u662f\u5982\u679c\u52a8\u6001\u6570\u636e\u6bd4\u8f83\u591a\uff0c\u5219\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u5927\u91cf\u7684\u52a0\u53f7\u548c\u5f15\u53f7\uff0c\u9605\u8bfb\u8d77\u6765\u4f1a\u6bd4\u8f83\u5403\u529b\uff0c\u5c24\u5176\u5728HTML\u62fc\u63a5\u7684\u4ee3\u7801\u4e2d\uff0c\u56e0\u4e3aHTML\u5c5e\u6027\u672c\u8eab\u5c31\u5305\u542b\u4e00\u5806\u5f15\u53f7\u3002<\/p>\n<p>\u4e8c\u662f\u5982\u679c\u5b57\u7b26\u4e32\u5305\u542b\u6362\u884c\u7b26\uff0c\u5219\u9700\u8981\u8f6c\u4e49\uff0c\u4e0d\u7136\u4f1a\u62a5\u9519\uff0c\u4f8b\u5982\u4e0b\u9762\u4ee3\u7801\uff0c\u6bcf\u4e00\u884c\u7684\u6700\u540e\u9700\u8981\u52a0\u4e00\u4e2a\u659c\u6760\uff0c\u4ee3\u7801\u53ef\u8bfb\u6027\u5b9e\u5728\u4e0d\u6562\u606d\u7ef4\uff1a<\/p>\n<pre>var html = '';\r\narrData.forEach(function (obj, index) {\r\n    html = html + '\\\r\n    &lt;tr&gt;\\\r\n        &lt;td&gt;&lt;input type=\"checkbox\" value=\"' + obj.id + '\"&gt;&lt;\/td&gt;\\\r\n        &lt;td&gt;&lt;div class=\"ell\"&gt;' + obj.title + '&lt;\/div&gt;&lt;\/td&gt;\\\r\n        &lt;td&gt;' + obj.time + '&lt;\/td&gt;\\\r\n        &lt;td align=\"right\"&gt;' + obj.comment + '&lt;\/td&gt;\r\n    &lt;\/tr&gt;\\\r\n';\r\n});\r\nconsole.log(html);<\/pre>\n<p>ES6\u6a21\u677f\u5b57\u7b26\u4e32\u53ef\u4ee5\u5927\u5927\u7f13\u89e3\u63d0\u9ad8\u7c7b\u4f3c\u9700\u6c42\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>let data = {\r\n    username: 'zhangxinxu'\r\n};\r\nconsole.log(`\u672c\u6587\u4f5c\u8005\u662f\uff1a${data.username}`);<\/pre>\n<p>\u53ef\u4ee5\u76f4\u63a5\u6362\u884c\uff0c\u4e0d\u9700\u8981\u62c5\u5fc3\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<pre>let html = `${data.map(function (obj, index) {\r\n  return `&lt;tr&gt;\r\n    &lt;td&gt;&lt;input type=\"checkbox\" value=\"${obj.id}\"&gt;&lt;\/td&gt;\r\n    &lt;td&gt;&lt;div class=\"ell\"&gt;${obj.title}&lt;\/div&gt;&lt;\/td&gt;\r\n    &lt;td&gt;${obj.time}&lt;\/td&gt;\r\n    &lt;td align=\"right\"&gt;${obj.comment}&lt;\/td&gt;\r\n  &lt;\/tr&gt;`;\r\n}).join('')}`;\r\n\r\nconsole.log(html);<\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u4e0a\u9762\u57fa\u4e8eES6\u6a21\u677f\u5b57\u7b26\u4e32\u62fc\u63a5HTML\u7247\u6bb5\u7684\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u660e\u663e\u63d0\u5347\u4e86\u5f88\u591a\uff0c\u5f15\u53f7\u4e5f\u5e72\u51c0\u4e86\uff0c\u6362\u884c\u4e5f\u65e0\u9700\u8f6c\u4e49\u76f4\u63a5\u4fdd\u7559\u4e86\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u8fd9\u4e2aES6\u6a21\u677f\u5b57\u7b26\u4e32\u4ecd\u7136\u6709\u4e0d\u8db3\u3002<\/p>\n<p>\u5e38\u89c4\u7684\u5b57\u7b26\u62fc\u63a5\u6ca1\u95ee\u9898\uff0c\u4f46\u662f\u5982\u679c\u662fHTML\u62fc\u63a5\uff0c\u7ef4\u62a4\u662f\u4e2a\u5927\u95ee\u9898\u3002<\/p>\n<p>\u56e0\u4e3aHTML\u4ee3\u7801\u8026\u5408\u5230\u4e86JavaScript\u4ee3\u7801\u4e2d\uff0c\u7406\u8bba\u4e0aHTML\u4ee3\u7801\u5c31\u5e94\u8be5\u5728HTML\u9875\u9762\u6216\u8005\u6a21\u677f\u9875\u9762\u4e2d\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u5c31\u601d\u8003\uff0c\u662f\u4e0d\u662f\u53ef\u4ee5\u4f7f\u7528ES6\u6a21\u677f\u5b57\u7b26\u4e32\u7684\u8bed\u6cd5\uff0c\u7136\u540eHTML\u6a21\u677f\u8fd8\u662f\u5728HTML\u9875\u9762\u4e2d\uff0c\u800c\u4e0d\u662f\u8026\u5408\u5728JavaScript\u4e2d\u3002<\/p>\n<h3>\u4e8c\u3001HTML\u6a21\u677f\u53d8\u6210JS\u6a21\u677f\u7684\u95ee\u9898<\/h3>\n<p>\u6bd4\u65b9\u8bf4\u9875\u9762\u4e0a\u6709\u4e0b\u9762\u8fd9\u6bb5HTML\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;table id=\"tableMix\" class=\"ui-table table\"&gt;\r\n&lt;thead&gt;\r\n  &lt;tr&gt;\r\n    &lt;th width=\"20\"&gt;&lt;input type=\"checkbox\"&gt;&lt;\/th&gt;\r\n    &lt;th&gt;\u6587\u7ae0\u6807\u9898&lt;\/th&gt;\r\n    &lt;th width=\"22%\"&gt;\u53d1\u5e03\u65f6\u95f4&lt;\/th&gt;\r\n    &lt;th width=\"15%\" align=\"right\"&gt;\u8bc4\u8bba\u6570&lt;\/th&gt;\r\n  &lt;\/tr&gt;\r\n&lt;\/thead&gt;\r\n&lt;tbody&gt;\r\n<span style=\"color:#cd0000;\">&lt;template&gt;\r\n${data.map(function (obj, index) {\r\n  return `&lt;tr&gt;\r\n    &lt;td&gt;&lt;input type=\"checkbox\" value=\"${obj.id}\"&gt;&lt;\/td&gt;\r\n    &lt;td&gt;&lt;div class=\"ell\"&gt;${obj.title}&lt;\/div&gt;&lt;\/td&gt;\r\n    &lt;td&gt;${obj.time}&lt;\/td&gt;\r\n    &lt;td align=\"right\"&gt;${obj.comment}&lt;\/td&gt;\r\n  &lt;\/tr&gt;`;\r\n}).join('')}\r\n&lt;\/template&gt;<\/span>\r\n&lt;\/tbody&gt;\r\n&lt;\/table&gt;<\/pre>\n<p>\u5176\u4e2d\u6709\u4e2a<code>&lt;template&gt;<\/code>\u5143\u7d20\uff0c\u653e\u7f6e\u4e86\u5217\u8868\u6e32\u67d3\u9700\u8981\u7684\u6a21\u677f\u6570\u636e\uff0c\u7136\u540e\u53d8\u91cf\u90e8\u5206\u4f7f\u7528\u4e86ES6\u7684\u6a21\u677f\u5b57\u7b26\u4e32\u8bed\u6cd5\u3002<\/p>\n<p>\u770b\u8d77\u6765\u4f3c\u4e4e\u65e0\u9700\u5f15\u5165\u7c7b\u4f3cartTemplate.js\u8fd9\u6837\u7684\u7b2c3\u65b9\u7684\u6e32\u67d3\u5f15\u64ce\u5c31\u53ef\u4ee5\u5b9e\u73b0\u6211\u4eec\u7684\u6570\u636e\u5c55\u793a\u6548\u679c\u4e86\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5f88\u9057\u61be\uff0c\u6211\u4eec\u4f7f\u7528DOM API\u83b7\u53d6\u5230\u7684<code>&lt;template&gt;<\/code>\u5143\u7d20\u4e2d\u7684\u5185\u5bb9\u662f\u4e00\u4e2a\u666e\u901a\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u6a21\u677f\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>console.log(document.querySelector('template').innerHTML);<\/pre>\n<p>\u8fd4\u56de\u7684\u662f\u666e\u901a\u7684\u5b57\u7b26\u4e32\u5185\u5bb9\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/2020-10-22_001442.png\" width=\"402\" height=\"178\" alt=\"\u8fd4\u56de\u7684\u662f\u666e\u901a\u5b57\u7b26\u4e32\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6b64\u65f6\u7684\u7c7b\u4f3c<code>${obj.id}<\/code>\u8fd9\u6837\u7684\u5185\u5bb9\u5c31\u662f\u4e2a\u666e\u901a\u5b57\u7b26\u4e32\uff0c\u5e76\u975e\u6a21\u677f\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u538b\u6839\u5c31\u6ca1\u529e\u6cd5\u8ba9\u91cc\u9762\u7684<code>${obj.id}<\/code>\u8fd9\u6837\u8bed\u6cd5\u7684\u5b57\u7b26\u53d8\u6210\u52a8\u6001\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u7f8e\u597d\u7684\u60f3\u6cd5\u4f3c\u4e4e\u9677\u5165\u4e86\u56f0\u5883\uff0c\u96be\u9053\u5c31\u6ca1\u6709\u529e\u6cd5\u8ba9\u666e\u901a\u7684\u5b57\u7b26\u4e32\u53d8\u6210\u6a21\u677f\u5b57\u7b26\u4e32\u5417\uff1f<\/p>\n<p>\u563f\uff01\u6709\u529e\u6cd5\u7684\u3002<\/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=9607\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<h3>\u4e09\u3001\u666e\u901a\u5b57\u7b26\u4e32\u8f6c\u6a21\u677f\u5b57\u7b26\u4e32<\/h3>\n<p>\u4e0b\u9762\u8fd9\u6bb5JavaScript\u4ee3\u7801\u53ef\u4ee5\u8ba9\u666e\u901a\u7684\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u677f\u5b57\u7b26\u4e32\u8bed\u6cd5\u8f6c\u6362\u6210\u5bf9\u5e94\u7684\u6570\u636e\u5e76\u8fd4\u56de\u51fa\u6765\u3002<\/p>\n<pre><span style=\"color:green;\">\/**\r\n * Convert a string to a template-string\r\n * @param  {Object} params \u6a21\u677f\u6570\u636e\r\n * @return {String}        \u6a21\u677f\u5b57\u7b26\u4e32\u8bed\u6cd5\u89e3\u6790\u540e\u7684\u5b57\u7b26\u4e32\r\n *\/<\/span>\r\nString.prototype.interpolate = function (params) {\r\n    const names = Object.keys(params);\r\n    const vals = Object.values(params);\r\n\r\n    return new Function(...names, `return \\`${this}\\`;`)(...vals);\r\n};<\/pre>\n<p>\u4e8e\u662f\uff0c\u4e0a\u9762\u5217\u8868\u7684HTML\u6a21\u677f\u5185\u5bb9\u60f3\u8981\u6e32\u67d3\u6210\u771f\u5b9e\u7684\u5217\u8868\u6570\u636e\uff0c\u53ea\u9700\u8981\u4e0b\u9762\u8fd9\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n<pre>let htmlList = document.querySelector('template').innerHTML.interpolate(json)<\/pre>\n<p>\u5176\u4e2d<code>json<\/code>\u5c31\u662f\u6a21\u677f\u6570\u636e\uff0c\u901a\u8fc7\u8bf7\u6c42\u540e\u7aef\u63a5\u53e3\u8fd4\u56de\u3002<\/p>\n<p>\u53ea\u770b\u4ee3\u7801\u53ef\u80fd\u4e0d\u592a\u6e05\u695a\u8bf4\u7684\u662f\u4ec0\u4e48\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202010\/es6-template-literals-as-html-demo.php\" rel=\"noopener\" target=\"_blank\">ES6\u6a21\u677f\u5b57\u7b26\u4e32\u8bed\u6cd5\u4f5c\u4e3aHTML\u6a21\u677fdemo<\/a><\/p>\n<p>\u4e0b\u56fe\u5c31\u662f\u4f7f\u7528<code>interpolate()<\/code>\u65b9\u6cd5\u8ba9\u4f7f\u7528\u4e86\u6a21\u677f\u5b57\u7b26\u4e32\u8bed\u6cd5\u7684HTML\u6a21\u677f\u53d8\u6210\u9700\u8981\u7684HTML\u4ee3\u7801\u7247\u6bb5\u540e\u7684\u6548\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202010\/2020-10-22_004205.png\" width=\"591\" height=\"175\" alt=\"\u5e73\u94fa\u7684\u6a21\u677f\u6548\u679c\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u76f8\u5e94\u7684JS\u4e1a\u52a1\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><span style=\"color:green;\">\/\/ \u4e1a\u52a1\u5904\u7406\u4ee3\u7801<\/span>\r\nvar eleTbody = document.querySelector('#tableMix tbody');\r\nvar strTemplate = eleTbody.querySelector('template').innerHTML;\r\n<span style=\"color:green;\">\/\/ \u83b7\u53d6\u6570\u636e<\/span>\r\nfetch('.\/ajax-article-list.php').then(res =&gt; {\r\n    return res.json();    \r\n}).then(json =&gt; {\r\n    <span style=\"color:#cd0000;\">eleTbody.innerHTML = strTemplate.interpolate(json);<\/span>    \r\n});<\/pre>\n<p>\u5176\u4e2djson\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre>{\r\n    \"code\": 0,\r\n    \"msg\": \"\u83b7\u53d6\u6210\u529f\",\r\n    \"data\": [{\r\n        \"id\": \"0001\",\r\n        \"title\": \"\u5982\u4f55\u8ba9\u6587\u5b57\u4f5c\u4e3aCSS\u80cc\u666f\u56fe\u7247\u663e\u793a\uff1f\",\r\n        \"time\": \"2020\u5e7410\u670820\u65e5\",\r\n        \"comment\": 7\r\n    }, {\r\n        \"id\": \"0002\",\r\n        \"title\": \"SVG feTurbulence\u6ee4\u955c\u6df1\u5165\u4ecb\u7ecd\",\r\n        \"time\": \"2020\u5e7410\u670817\u65e5\",\r\n        \"comment\": 3\r\n    }, {\r\n        \"id\": \"0003\",\r\n        \"title\": \"HTML enterkeyhint\u8bbe\u7f6eiOS\/Android\u952e\u76d8enter\u952e\",\r\n        \"time\": \"2020\u5e7410\u670811\u65e5\",\r\n        \"comment\": 3\r\n    }, {\r\n        \"id\": \"0004\",\r\n        \"title\": \"\u5feb\u901f\u5b66\u4e60CSS Color Level 4\u7684\u8272\u503c\u65b0\u8bed\u6cd5\",\r\n        \"time\": \"2020\u5e7410\u670811\u65e5\",\r\n        \"comment\": 2\r\n    }]    \r\n}<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u4e1a\u52a1\u4ee3\u7801\u7684\u5b9e\u73b0\u975e\u5e38\u7684\u7b80\u6d01\u3002<\/p>\n<p>\u4e8e\u662f\u4e4e\uff0c\u4e00\u4e2a\u5b8c\u5168\u4e0d\u9700\u8981\u5e94\u7528\u7b2c3\u65b9\u6a21\u677f\u6e32\u67d3\u5f15\u64ce\uff0c\u4e5f\u65e0\u9700\u5b66\u4e60\u4efb\u4f55\u6a21\u677f\u8bed\u6cd5\uff08\u56e0\u4e3a\u4f7f\u7528\u7684\u662f\u539f\u751fES6\u6a21\u677f\u5b57\u7b26\u4e32\u8bed\u6cd5\uff09\u7684HTML\u6a21\u677f\u6e32\u67d3\u529f\u80fd\u5c31\u5b9e\u73b0\u4e86\u3002<\/p>\n<p>\u7279\u522b\u9002\u5408\u7528\u5728\u975e\u957f\u671f\u7684\uff0c\u9700\u8981\u6377\u5f00\u53d1\u7684\u9879\u76ee\u4e2d\uff0c\u4f8b\u5982\u8fd0\u8425\u6d3b\u52a8\u9875\u9762\uff0c\u4e00\u4e9b\u5de5\u5177\u7c7b\u9875\u9762\uff0c\u504f\u5c55\u793a\u6027\u7684\u7f51\u7ad9\u7b49\u3002<\/p>\n<h3>\u56db\u3001\u4e00\u4e9b\u9650\u5236<\/h3>\n<p>\u8fd9\u79cd\u5229\u7528\u539f\u751f\u6a21\u677f\u5b57\u7b26\u4e32\u8bed\u6cd5\u8fdb\u884cHTML\u6a21\u677f\u89e3\u6790\u7684\u6280\u672f\u867d\u7136\u7b80\u6d01\u9ad8\u6548\uff0c\u4f46\u662f\u4e5f\u662f\u6709\u4e00\u4e9b\u9650\u5236\u7684\u3002<\/p>\n<p>\u9996\u5148\u662f\u517c\u5bb9\u6027\u7684\u9650\u5236\u3002<\/p>\n<p>\u5982\u679c\u9879\u76ee\u9700\u8981\u517c\u5bb9IE\u6d4f\u89c8\u5668\uff0c\u5219\u9700\u8981\u659f\u914c\u4e00\u4e0b\u4e86\uff0c\u6211\u8fd8\u4e0d\u786e\u5b9aBabel\u8f6c\u6210ES5\u8bed\u6cd5\u540e\u529f\u80fd\u662f\u5426\u8fd8\u6b63\u5e38\u3002<\/p>\n<p>\u53e6\u5916\u4e00\u4e2a\u9650\u5236\u662f\u4e0d\u80fd\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff08\u6ce8\u610f\uff0c\u662f\u4f5c\u4e3aHTML\u6a21\u677f\u5b57\u7b26\u4e32\u5185\u5bb9\u65f6\u5019\u4e0d\u80fd\u4f7f\u7528\u7bad\u5934\u51fd\u6570\uff0c\u5728JavaScript\u4e2d\u8fd8\u662f\u53ef\u4ee5\u7684\uff09\uff0c\u53ea\u652f\u6301\u5e38\u89c4\u7684\u8868\u8fbe\u5f0f\u8bed\u6cd5\u3002<\/p>\n<p>\u4f8b\u5982\u4e0b\u9762\u7684\u5199\u6cd5\u5c31\u4f1a\u62a5\u9519\uff0c\u53ea\u80fd\u4f7f\u7528<code>function<\/code>\u51fd\u6570\uff1a<\/p>\n<pre><span style=\"color:#cd0000;\">\/* \u62a5\u9519 *\/<\/span>\r\n${data.map((obj, index) =&gt; {\r\n  return `&lt;tr&gt;\r\n    ...\r\n  &lt;\/tr&gt;`;\r\n}).join('')}<\/pre>\n<p><strong>\u66f4\u65b0\u4e8e2020-11-17<\/strong><\/p>\n<p>\u6b64\u6a21\u677f\u8f6c\u6362\u65b9\u6cd5\u662f\u652f\u6301\u7bad\u5934\u51fd\u6570\u7684\uff0c\u524d\u63d0\u662f\u9700\u8981\u53cd\u8f6c\u4e49\u4e0b\uff08by XboxYan\uff09\u3002<\/p>\n<pre>\/\/ HTML\u5b57\u7b26\u53cd\u8f6c\u4e49   &amp;lt;  =&gt;  &lt; \r\nfunction escape2Html(str) {\r\n    var arrEntities = { 'lt': '&lt;', 'gt': '&gt;', 'nbsp': ' ', 'amp': '&amp;', 'quot': '\"' };\r\n    return str.replace(\/&amp;(lt|gt|nbsp|amp|quot);\/ig, function (all, t) {\r\n        return arrEntities[t];\r\n    });\r\n}\r\nString.prototype.interpolate = function (params) {\r\n    const names = Object.keys(params);\r\n    const vals = Object.values(params);\r\n    return new Function(...names, `return \\`${escape2Html(this)}\\`;`)(...vals);\r\n};\r\n<\/pre>\n<h3>\u4e94\u3001\u8fdb\u4e00\u6b65\u7684\u6a21\u677f\u6e32\u67d3\u65b9\u6cd5<\/h3>\n<p>\u6211\u7684\u540c\u4e8b<a href=\"https:\/\/github.com\/XboxYan\">XboxYan<\/a>\u5728\u672c\u6587\u7684<code>interpolate()<\/code>\u57fa\u7840\u4e0a\u8fdb\u4e00\u6b65\u5ef6\u4f38\uff0c\u5b9e\u73b0\u4e86\u4e00\u4e2a\u6781\u7b80\u7684\u6a21\u677f\u6e32\u67d3\u5f15\u64ce\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/XboxYan\/web-template\" target=\"_blank\" rel=\"nooppener nofollow\">https:\/\/github.com\/XboxYan\/web-template<\/a><\/p>\n<p>\u7279\u70b9\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u7eaf\u539f\u751f\u6d4f\u89c8\u5668\u89e3\u6790\uff0c\u65e0\u4efb\u4f55\u4f9d\u8d56\uff0c\u65e0\u9700\u7f16\u8bd1\uff0c\u4e0d\u62d6\u6ce5\u5e26\u6c34<\/li>\n<li>\u7c7b vue \u6a21\u677f\u8bed\u6cd5\uff0c\u4e0a\u624b\u5feb\uff0c\u51e0\u4e4e\u53ef\u4ee5\u4e0d\u7528\u770b\u6587\u6863<\/li>\n<li>\u4ee3\u7801\u91cf\u6781\u5c11\uff0c\u5305\u542b\u6ce8\u91ca\u4e0d\u5230 100 \u884c\uff0c\u65b9\u4fbf\u5b66\u4e60\u548c\u6269\u5c55<\/li>\n<\/ol>\n<p>\u6b22\u8fce\u54c1\u9274\uff01<\/p>\n<h3>\u516d\u3001\u5176\u4ed6\u4ee5\u53ca\u7ed3\u8bed<\/h3>\n<p>\u672c\u6587\u7684demo\u4f7f\u7528\u7684<code>&lt;template&gt;<\/code>\u5143\u7d20\u91cc\u9762\u7684\u5185\u5bb9\u672c\u8eab\u662f\u53ef\u4ee5\u5177\u6709DOM\u7279\u6027\u83b7\u53d6\u7684\uff0c\u662fHTML5\u4e2d\u7684\u4e00\u4e2a\u89c4\u8303\u6807\u7b7e\uff0cIE\u6d4f\u89c8\u5668\u4e0d\u652f\u6301\uff0c\u5305\u542b\u5176\u4ed6\u7279\u6027\uff0c\u4e0d\u4ec5\u4ec5\u662f\u91cc\u9762\u5185\u5bb9\u4f5c\u4e3a\u5b57\u7b26\u4e32\u83b7\u53d6\uff0c\u66f4\u8be6\u7ec6\u5185\u5bb9\u53ef\u4ee5\u53c2\u89c1\u8fd9\u7bc7\u6587\u7ae0\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2014\/07\/hello-html5-template-tag\/\">HTML5 &lt;template&gt;\u6807\u7b7e\u5143\u7d20\u7b80\u4ecb<\/a>\u201d\u3002<\/p>\n<p>\u6a21\u677f\u5b57\u7b26\u4e32\uff08\u53c8\u79f0\u6a21\u677f\u5b57\u9762\u91cf &#8211; Template literals\uff09\u8fd8\u6709\u4e2a\u6807\u7b7e\u6a21\u677f\u7684\u7279\u6027\uff0c\u4e5f\u5c31\u662f\u6a21\u677f\u5b57\u7b26\u4e32\u5982\u679c\u7d27\u8ddf\u5728\u51fd\u6570\u540e\u9762\uff0c\u5219\u8fd9\u4e2a\u51fd\u6570\u5c06\u88ab\u8c03\u7528\u6765\u5904\u7406\u8fd9\u4e2a\u6a21\u677f\u5b57\u7b26\u4e32\uff0c\u4f5c\u7528\u662f\u5bf9\u6a21\u677f\u6570\u636e\u8fdb\u884c\u7cbe\u786e\u63a7\u5236\u4e0e\u62fc\u63a5\u3002\u4e0d\u8fc7\u8fd9\u4e2a\u7279\u6027\u4e0e\u672c\u6587\u5185\u5bb9\u5173\u7cfb\u4e0d\u5927\uff0c\u540c\u65f6\u81ea\u5df1\u4e5f\u6ca1\u6709\u53d1\u73b0\u8fd9\u4e2a\u7279\u6027\u6709\u7279\u522b\u5b9e\u7528\u7684\u5e94\u7528\u573a\u666f\uff0c\u56e0\u6b64\u8fd9\u91cc\u4e0d\u505a\u5c55\u5f00\u4ecb\u7ecd\u3002<\/p>\n<p>\u6709\u5174\u8da3\u53ef\u4ee5\u8bbf\u95ee<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Template_literals\" rel=\"noopener\" target=\"_blank\">\u8fd9\u91ccMDN\u6587\u6863<\/a>\u5173\u4e8e\u6a21\u677f\u5b57\u9762\u91cf\u7684\u4ecb\u7ecd\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5176\u5b9e\u65e9\u57282012\u5e74\u7684\u65f6\u5019\uff0c\u9762\u5411\u6570\u636e\u7684\u7f16\u7a0b\u6587\u7ae0\u6211\u5c31\u5199\u8fc7\u51e0\u7bc7\uff0c\u4f8b\u5982\u5176\u4e2d\u4e00\u7bc7\u662f\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2012\/09\/javascript-html-json-template\/\">\u57fa\u4e8eHTML\u6a21\u677f\u548cJSON\u6570\u636e\u7684JavaScript\u4ea4\u4e92<\/a>\u201d\uff0c\u76ee\u524d\u5df2\u7ecf\u670920\u591a\u4e07\u7684\u8bbf\u95ee\u91cf\u4e86\u3002\u8fd9\u7bc7\u6587\u7ae0\u7684\u6838\u5fc3\u4e0e\u672c\u6587\u5176\u5b9e\u662f\u7c7b\u4f3c\u7684\uff0c\u4f46\u662f\u5b9e\u73b0\u5f62\u5f0f\u4e0a\u6709\u4e86\u5347\u7ea7\uff0c\u4ee5\u524d\u4f7f\u7528\u7684\u662f<code>&lt;textarea&gt;<\/code>\u5143\u7d20\u4f5c\u4e3aHTML\u6a21\u677f\u5bb9\u5668\uff0c\u73b0\u5728\u4f7f\u7528\u7684\u662f<code>&lt;template&gt;<\/code>\u5143\u7d20\uff1b\u4ee5\u524d\u4f7f\u7528\u7684\u662f\u81ea\u5df1\u81ea\u5b9a\u4e49\u7684<code>$$<\/code>\u8bed\u6cd5\uff0c\u4e14\u4ec5\u652f\u6301\u7b80\u5355\u7684\u5bf9\u8c61\u5b57\u9762\u91cf\u89e3\u6790\uff1b\u73b0\u5728\u4f7f\u7528\u7684\u662f\u6d4f\u89c8\u5668\u539f\u751f\u7684ES6\u6a21\u677f\u5b57\u7b26\u4e32\u8bed\u6cd5\uff0c\u4e14\u652f\u6301\u5faa\u73af\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\u601d\u8def\u548c\u539f\u7406\u6ca1\u53d8\uff0c\u6280\u672f\u7684\u53d8\u5316\u5e26\u6765\u7684\u5b9e\u73b0\u4e0a\u7684\u63d0\u5347\u3002<\/p>\n<p>\u597d\uff0c\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u8ba9\u6a21\u677f\u5b57\u7b26\u4e32\u7684\u80fd\u529b\u5ef6\u4f38\u5230HTML\u6a21\u677f\u5b57\u7b26\u4e32\u4e2d\uff0c\u4ece\u800c\u8282\u7ea6\u6211\u4eec\u7684\u5f00\u53d1\u6210\u672c\uff0c\u53d1\u6325JS\u65b0\u7279\u6027\u7684\u6f5c\u529b\u3002<\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff0c\u6b22\u8fce\u5206\u4eab\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<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=9607\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9607<\/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\/202010\/es6-html-template-cover.png\" width=\"160\" height=\"103\" alt=\"\u6a21\u677f\u5b57\u9762\u91cfHTML\u6e32\u67d3\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>ES6\u6a21\u677f\u5b57\u7b26\u4e32\u81ea\u5e26\u6a21\u677f\u89e3\u6790\u7279\u6027\uff0c\u6709\u6ca1\u6709\u53ef\u80fdHTML\u6a21\u677f\u76f4\u63a5\u4f7f\u7528ES6\u6a21\u677f\u5b57\u9762\u91cf\u8bed\u6cd5\uff0c\u7136\u540e\u8fdb\u884c\u6e32\u67d3\u89e3\u6790\u5462\uff0c\u8fd9\u6837\u5c31\u4e0d\u9700\u8981\u5f15\u7528\u7b2c3\u65b9\u6a21\u677f\u6e32\u67d3\u5f15\u64ce\u4e86\uff0c\u5c82\u4e0d\u5999\u54c9\uff01\u4f46\u662f\u5b9e\u64cd\u4e0b\u6765\u6ca1\u90a3\u4e48\u7b80\u5355\uff0c\u597d\u5728\u6211\u627e\u5230\u4e86\u4e00\u4e2a\u89e3\u51b3\u65b9\u6cd5\u3002\u672c\u6587\u7684\u6280\u672f\u540c\u6837\u5b9e\u7528\uff0c\u6709demo\u6709\u622a\u56fe\uff0c\u5e0c\u671b\u672c\u6587\u5185\u5bb9\u53ef\u4ee5\u5bf9\u60a8\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":[180],"tags":[904,755,13,928,1689,1683,1684],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9607"}],"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=9607"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9607\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}