{"id":752,"date":"2024-07-04T20:47:34","date_gmt":"2024-07-04T12:47:34","guid":{"rendered":"https:\/\/levin.ink\/?p=752"},"modified":"2024-07-04T20:47:34","modified_gmt":"2024-07-04T12:47:34","slug":"js-3-21-web-components","status":"publish","type":"post","link":"https:\/\/levin.ink\/?p=752","title":{"rendered":"JS &#8211; 3-21 Web components"},"content":{"rendered":"<h1>Custom elements<\/h1>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u63cf\u8ff0\u5e26\u6709\u81ea\u5df1\u7684\u65b9\u6cd5\u3001\u5c5e\u6027\u548c\u4e8b\u4ef6\u7b49\u7684\u7c7b\u6765\u521b\u5efa\u81ea\u5b9a\u4e49 HTML \u5143\u7d20\u3002<br \/>\n\u5728 custom elements \uff08\u81ea\u5b9a\u4e49\u6807\u7b7e\uff09\u5b9a\u4e49\u5b8c\u6210\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u548c HTML \u7684\u5185\u5efa\u6807\u7b7e\u4e00\u540c\u4f7f\u7528\u3002<br \/>\n\u8fd9\u662f\u4e00\u4ef6\u597d\u4e8b\uff0c\u56e0\u4e3a\u867d\u7136 HTML \u6709\u975e\u5e38\u591a\u7684\u6807\u7b7e\uff0c\u4f46\u4ecd\u7136\u662f\u6709\u7a77\u5c3d\u7684\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u50cf\u00a0<code>&lt;easy-tabs&gt;<\/code>\u3001<code>&lt;sliding-carousel&gt;<\/code>\u3001<code>&lt;beautiful-upload&gt;<\/code>\u2026\u2026 \u8fd9\u6837\u7684\u6807\u7b7e\uff0c\u5185\u5efa\u6807\u7b7e\u5e76\u4e0d\u80fd\u6ee1\u8db3\u6211\u4eec\u3002<br \/>\n\u6211\u4eec\u53ef\u4ee5\u628a\u4e0a\u8ff0\u7684\u6807\u7b7e\u5b9a\u4e49\u4e3a\u7279\u6b8a\u7684\u7c7b\uff0c\u7136\u540e\u4f7f\u7528\u5b83\u4eec\uff0c\u5c31\u597d\u50cf\u5b83\u4eec\u672c\u6765\u5c31\u662f HTML \u7684\u4e00\u90e8\u5206\u4e00\u6837\u3002<br \/>\nCustom elements \u6709\u4e24\u79cd\uff1a<\/p>\n<ol>\n<li><strong>Autonomous custom elements \uff08\u81ea\u4e3b\u81ea\u5b9a\u4e49\u6807\u7b7e\uff09<\/strong>\u00a0\u2014\u2014 \u201c\u5168\u65b0\u7684\u201d \u5143\u7d20, \u7ee7\u627f\u81ea\u00a0<code>HTMLElement<\/code>\u00a0\u62bd\u8c61\u7c7b.<\/li>\n<li><strong>Customized built-in elements \uff08\u81ea\u5b9a\u4e49\u5185\u5efa\u5143\u7d20\uff09<\/strong>\u00a0\u2014\u2014 \u7ee7\u627f\u5185\u5efa\u7684 HTML \u5143\u7d20\uff0c\u6bd4\u5982\u81ea\u5b9a\u4e49\u00a0<code>HTMLButtonElement<\/code>\u00a0\u7b49\u3002<br \/>\n\u6211\u4eec\u5c06\u4f1a\u5148\u521b\u5efa autonomous \u5143\u7d20\uff0c\u7136\u540e\u518d\u521b\u5efa customized built-in \u5143\u7d20\u3002<br \/>\n\u5728\u521b\u5efa custom elements \u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u544a\u8bc9\u6d4f\u89c8\u5668\u4e00\u4e9b\u7ec6\u8282\uff0c\u5305\u62ec\uff1a\u5982\u4f55\u5c55\u793a\u5b83\uff0c\u4ee5\u53ca\u5728\u6dfb\u52a0\u5143\u7d20\u5230\u9875\u9762\u548c\u5c06\u5176\u4ece\u9875\u9762\u79fb\u9664\u7684\u65f6\u5019\u9700\u8981\u505a\u4ec0\u4e48\uff0c\u7b49\u7b49\u3002<br \/>\n\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5e26\u6709\u51e0\u4e2a\u7279\u6b8a\u65b9\u6cd5\u7684\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u5b8c\u6210\u8fd9\u4ef6\u4e8b\u3002\u8fd9\u975e\u5e38\u5bb9\u6613\u5b9e\u73b0\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6dfb\u52a0\u51e0\u4e2a\u65b9\u6cd5\u5c31\u884c\u4e86\uff0c\u540c\u65f6\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u4e0d\u662f\u5fc5\u987b\u7684\u3002<br \/>\n\u4e0b\u9762\u5217\u51fa\u4e86\u8fd9\u51e0\u4e2a\u65b9\u6cd5\u7684\u6982\u8ff0\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-js\">class MyElement extends HTMLElement {\n  constructor() {\n    super();\n    \/\/ \u5143\u7d20\u5728\u8fd9\u91cc\u521b\u5efa\n  }\n\n  connectedCallback() {\n    \/\/ \u5728\u5143\u7d20\u88ab\u6dfb\u52a0\u5230\u6587\u6863\u4e4b\u540e\uff0c\u6d4f\u89c8\u5668\u4f1a\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\n    \/\/\uff08\u5982\u679c\u4e00\u4e2a\u5143\u7d20\u88ab\u53cd\u590d\u6dfb\u52a0\u5230\u6587\u6863\uff0f\u79fb\u9664\u6587\u6863\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u88ab\u591a\u6b21\u8c03\u7528\uff09\n  }\n\n  disconnectedCallback() {\n    \/\/ \u5728\u5143\u7d20\u4ece\u6587\u6863\u79fb\u9664\u7684\u65f6\u5019\uff0c\u6d4f\u89c8\u5668\u4f1a\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\n    \/\/ \uff08\u5982\u679c\u4e00\u4e2a\u5143\u7d20\u88ab\u53cd\u590d\u6dfb\u52a0\u5230\u6587\u6863\uff0f\u79fb\u9664\u6587\u6863\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u88ab\u591a\u6b21\u8c03\u7528\uff09\n  }\n\n  static get observedAttributes() {\n    return [\/* \u5c5e\u6027\u6570\u7ec4\uff0c\u8fd9\u4e9b\u5c5e\u6027\u7684\u53d8\u5316\u4f1a\u88ab\u76d1\u89c6 *\/];\n  }\n\n  attributeChangedCallback(name, oldValue, newValue) {\n    \/\/ \u5f53\u4e0a\u9762\u6570\u7ec4\u4e2d\u7684\u5c5e\u6027\u53d1\u751f\u53d8\u5316\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u88ab\u8c03\u7528\n  }\n\n  adoptedCallback() {\n    \/\/ \u5728\u5143\u7d20\u88ab\u79fb\u52a8\u5230\u65b0\u7684\u6587\u6863\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u88ab\u8c03\u7528\n    \/\/ \uff08document.adoptNode \u4f1a\u7528\u5230, \u975e\u5e38\u5c11\u89c1\uff09\n  }\n\n  \/\/ \u8fd8\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u7684\u5143\u7d20\u65b9\u6cd5\u548c\u5c5e\u6027\n}\n<\/code><\/pre>\n<p>\u5728\u7533\u660e\u4e86\u4e0a\u9762\u51e0\u4e2a\u65b9\u6cd5\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u6ce8\u518c\u5143\u7d20\uff1a<\/p>\n<pre><code class=\"language-js\">\/\/ \u8ba9\u6d4f\u89c8\u5668\u77e5\u9053\u6211\u4eec\u65b0\u5b9a\u4e49\u7684\u7c7b\u662f\u4e3a &lt;my-element&gt; \u670d\u52a1\u7684\ncustomElements.define(&quot;my-element&quot;, MyElement);\n<\/code><\/pre>\n<p>\u73b0\u5728\u5f53\u4efb\u4f55\u5e26\u6709\u00a0<code>&lt;my-element&gt;<\/code>\u00a0\u6807\u7b7e\u7684\u5143\u7d20\u88ab\u521b\u5efa\u7684\u65f6\u5019\uff0c\u4e00\u4e2a\u00a0<code>MyElement<\/code>\u00a0\u7684\u5b9e\u4f8b\u4e5f\u4f1a\u88ab\u521b\u5efa\uff0c\u5e76\u4e14\u524d\u9762\u63d0\u5230\u7684\u65b9\u6cd5\u4e5f\u4f1a\u88ab\u8c03\u7528\u3002\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u4f7f\u7528\u00a0<code>document.createElement('my-element')<\/code>\u00a0\u5728 JavaScript \u91cc\u521b\u5efa\u5143\u7d20\u3002<\/p>\n<blockquote>\n<p>Custom element \u540d\u79f0\u5fc5\u987b\u5305\u62ec\u4e00\u4e2a\u77ed\u6a2a\u7ebf\u00a0<code>-<\/code><br \/>\nCustom element \u540d\u79f0\u5fc5\u987b\u5305\u62ec\u4e00\u4e2a\u77ed\u6a2a\u7ebf\u00a0<code>-<\/code>, \u6bd4\u5982\u00a0<code>my-element<\/code>\u00a0\u548c\u00a0<code>super-button<\/code>\u00a0\u90fd\u662f\u6709\u6548\u7684\u5143\u7d20\u540d\uff0c\u4f46\u00a0<code>myelement<\/code>\u00a0\u5e76\u4e0d\u662f\u3002<br \/>\n\u8fd9\u662f\u4e3a\u4e86\u786e\u4fdd custom element \u548c\u5185\u5efa HTML \u5143\u7d20\u4e4b\u95f4\u4e0d\u4f1a\u53d1\u751f\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/blockquote>\n<h2>\u6e32\u67d3\u987a\u5e8f<\/h2>\n<p>\u5728 HTML \u89e3\u6790\u5668\u6784\u5efa DOM \u7684\u65f6\u5019\uff0c\u4f1a\u6309\u7167\u5148\u540e\u987a\u5e8f\u5904\u7406\u5143\u7d20\uff0c\u5148\u5904\u7406\u7236\u7ea7\u5143\u7d20\u518d\u5904\u7406\u5b50\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u00a0<code>&lt;outer&gt;&lt;inner&gt;&lt;\/inner&gt;&lt;\/outer&gt;<\/code>\uff0c\u90a3\u4e48\u00a0<code>&lt;outer&gt;<\/code>\u00a0\u5143\u7d20\u4f1a\u9996\u5148\u88ab\u521b\u5efa\u5e76\u63a5\u5165\u5230 DOM\uff0c\u7136\u540e\u624d\u662f\u00a0<code>&lt;inner&gt;<\/code>\u3002<br \/>\n\u8fd9\u5bf9 custom elements \u4ea7\u751f\u4e86\u91cd\u8981\u5f71\u54cd\u3002<br \/>\n\u6bd4\u5982\uff0c\u5982\u679c\u4e00\u4e2a custom element \u60f3\u8981\u5728\u00a0<code>connectedCallback<\/code>\u00a0\u5185\u8bbf\u95ee\u00a0<code>innerHTML<\/code>\uff0c\u5b83\u4ec0\u4e48\u4e5f\u62ff\u4e0d\u5230:<\/p>\n<pre><code class=\"language-html\">&lt;script&gt;\ncustomElements.define('user-info', class extends HTMLElement {\n\n  connectedCallback() {\n    alert(this.innerHTML); \/\/ empty (*)\n  }\n\n});\n&lt;\/script&gt;\n\n&lt;user-info&gt;John&lt;\/user-info&gt;\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c<code>alert<\/code>\u00a0\u51fa\u6765\u7684\u5185\u5bb9\u662f\u7a7a\u7684\u3002<br \/>\n\u8fd9\u6b63\u662f\u56e0\u4e3a\u5728\u90a3\u4e2a\u9636\u6bb5\uff0c\u5b50\u5143\u7d20\u8fd8\u4e0d\u5b58\u5728\uff0cDOM \u8fd8\u6ca1\u6709\u5b8c\u6210\u6784\u5efa\u3002HTML \u89e3\u6790\u5668\u5148\u8fde\u63a5 custom element\u00a0<code>&lt;user-info&gt;<\/code>\uff0c\u7136\u540e\u518d\u5904\u7406\u5b50\u5143\u7d20\uff0c\u4f46\u662f\u90a3\u65f6\u5019\u5b50\u5143\u7d20\u8fd8\u5e76\u6ca1\u6709\u52a0\u8f7d\u4e0a\u3002<br \/>\n\u5982\u679c\u6211\u4eec\u8981\u7ed9 custom element \u4f20\u5165\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5143\u7d20\u5c5e\u6027\u3002\u5b83\u4eec\u662f\u5373\u65f6\u751f\u6548\u7684\u3002<br \/>\n\u6216\u8005\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u5b50\u5143\u7d20\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5ef6\u8fdf\u65f6\u95f4\u4e3a\u96f6\u7684\u00a0<code>setTimeout<\/code>\u00a0\u6765\u63a8\u8fdf\u8bbf\u95ee\u5b50\u5143\u7d20\u3002<br \/>\n\u8fd9\u6837\u662f\u53ef\u884c\u7684\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;script&gt;\ncustomElements.define('user-info', class extends HTMLElement {\n\n  connectedCallback() {\n    setTimeout(() =&gt; alert(this.innerHTML)); \/\/ John (*)\n  }\n\n});\n&lt;\/script&gt;\n\n&lt;user-info&gt;John&lt;\/user-info&gt;\n<\/code><\/pre>\n<p>\u73b0\u5728\u00a0<code>alert<\/code>\u00a0\u5728\u00a0<code>(*)<\/code>\u00a0\u884c\u5c55\u793a\u4e86 \u300cJohn\u300d\uff0c\u56e0\u4e3a\u6211\u4eec\u662f\u5728 HTML \u89e3\u6790\u5b8c\u6210\u4e4b\u540e\uff0c\u624d\u5f02\u6b65\u6267\u884c\u4e86\u8fd9\u6bb5\u7a0b\u5e8f\u3002\u6211\u4eec\u5728\u8fd9\u4e2a\u65f6\u5019\u5904\u7406\u5fc5\u8981\u7684\u5b50\u5143\u7d20\u5e76\u4e14\u7ed3\u675f\u521d\u59cb\u5316\u8fc7\u7a0b\u3002<br \/>\n\u53e6\u4e00\u65b9\u9762\uff0c\u8fd9\u4e2a\u65b9\u6848\u5e76\u4e0d\u662f\u5b8c\u7f8e\u7684\u3002\u5982\u679c\u5d4c\u5957\u7684 custom element \u540c\u6837\u4f7f\u7528\u4e86\u00a0<code>setTimeout<\/code>\u00a0\u6765\u521d\u59cb\u5316\u81ea\u8eab\uff0c\u90a3\u4e48\u5b83\u4eec\u4f1a\u6309\u7167\u5148\u540e\u987a\u5e8f\u6267\u884c\uff1a\u5916\u5c42\u7684\u00a0<code>setTimeout<\/code>\u00a0\u9996\u5148\u89e6\u53d1\uff0c\u7136\u540e\u624d\u662f\u5185\u5c42\u7684\u3002<br \/>\n\u8fd9\u6837\u5916\u5c42\u5143\u7d20\u8fd8\u662f\u65e9\u4e8e\u5185\u5c42\u5143\u7d20\u7ed3\u675f\u521d\u59cb\u5316\u3002<\/p>\n<pre><code class=\"language-html\">&lt;script&gt;\ncustomElements.define('user-info', class extends HTMLElement {\n  connectedCallback() {\n    alert(`${this.id} \u5df2\u8fde\u63a5\u3002`);\n    setTimeout(() =&gt; alert(`${this.id} \u521d\u59cb\u5316\u5b8c\u6210\u3002`));\n  }\n});\n&lt;\/script&gt;\n\n&lt;user-info id=&quot;outer&quot;&gt;\n  &lt;user-info id=&quot;inner&quot;&gt;&lt;\/user-info&gt;\n&lt;\/user-info&gt;\n<\/code><\/pre>\n<p>\u8f93\u51fa\u987a\u5e8f\uff1a<\/p>\n<ol>\n<li>outer \u5df2\u8fde\u63a5\u3002<\/li>\n<li>inner \u5df2\u8fde\u63a5\u3002<\/li>\n<li>outer \u521d\u59cb\u5316\u5b8c\u6210\u3002<\/li>\n<li>inner \u521d\u59cb\u5316\u5b8c\u6210\u3002<br \/>\n\u6211\u4eec\u53ef\u4ee5\u5f88\u660e\u663e\u5730\u770b\u5230\u5916\u5c42\u5143\u7d20\u5e76\u6ca1\u6709\u7b49\u5f85\u5185\u5c42\u5143\u7d20\u3002<br \/>\n\u5e76\u6ca1\u6709\u4efb\u4f55\u5185\u5efa\u7684\u56de\u8c03\u65b9\u6cd5\u53ef\u4ee5\u5728\u5d4c\u5957\u5143\u7d20\u6e32\u67d3\u597d\u4e4b\u540e\u901a\u77e5\u6211\u4eec\u3002\u4f46\u6211\u4eec\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0\u8fd9\u6837\u7684\u56de\u8c03\u3002\u6bd4\u5982\uff0c\u5185\u5c42\u5143\u7d20\u53ef\u4ee5\u5206\u6d3e\u50cf\u00a0<code>initialized<\/code>\u00a0\u8fd9\u6837\u7684\u4e8b\u4ef6\uff0c\u540c\u65f6\u5916\u5c42\u7684\u5143\u7d20\u76d1\u542c\u8fd9\u6837\u7684\u4e8b\u4ef6\u5e76\u505a\u51fa\u54cd\u5e94\u3002<\/li>\n<\/ol>\n<h2>Customized built-in elements<\/h2>\n<p>\u6211\u4eec\u521b\u5efa\u7684\u00a0<code>&lt;time-formatted&gt;<\/code>\u00a0\u8fd9\u4e9b\u65b0\u5143\u7d20\uff0c\u5e76\u6ca1\u6709\u4efb\u4f55\u76f8\u5173\u7684\u8bed\u4e49\u3002\u641c\u7d22\u5f15\u64ce\u5e76\u4e0d\u77e5\u6653\u5b83\u4eec\u7684\u5b58\u5728\uff0c\u540c\u65f6\u65e0\u969c\u788d\u8bbe\u5907\u4e5f\u65e0\u6cd5\u5904\u7406\u5b83\u4eec\u3002<br \/>\n\u4f46\u4e0a\u8ff0\u4e24\u70b9\u540c\u6837\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u6bd4\u5982\uff0c\u641c\u7d22\u5f15\u64ce\u4f1a\u5bf9\u8fd9\u4e9b\u4e8b\u60c5\u611f\u5174\u8da3\uff0c\u6bd4\u5982\u6211\u4eec\u771f\u7684\u5c55\u793a\u4e86\u65f6\u95f4\u3002\u6216\u8005\u5982\u679c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7279\u522b\u7684\u6309\u94ae\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u590d\u7528\u5df2\u6709\u7684\u00a0<code>&lt;button&gt;<\/code>\u00a0\u529f\u80fd\u5462\uff1f<br \/>\n\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u5185\u5efa\u5143\u7d20\u7684\u7c7b\u6765\u6269\u5c55\u548c\u5b9a\u5236\u5b83\u4eec\u3002<br \/>\n\u6bd4\u5982\uff0c\u6309\u94ae\u662f\u00a0<code>HTMLButtonElement<\/code>\u00a0\u7684\u5b9e\u4f8b\uff0c\u8ba9\u6211\u4eec\u5728\u8fd9\u4e2a\u57fa\u7840\u4e0a\u521b\u5efa\u5143\u7d20\u3002<\/p>\n<ol>\n<li>\u6211\u4eec\u7684\u7c7b\u7ee7\u627f\u81ea\u00a0<code>HTMLButtonElement<\/code>\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-js\">class HelloButton extends HTMLButtonElement { \/* custom element \u65b9\u6cd5 *\/ }\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u7ed9\u00a0<code>customElements.define<\/code>\u00a0\u63d0\u4f9b\u5b9a\u4e49\u6807\u7b7e\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-js\">customElements.define('hello-button', HelloButton, {extends: 'button'});\n<\/code><\/pre>\n<p>\u8fd9\u4e00\u6b65\u662f\u5fc5\u8981\u7684\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u6807\u7b7e\u4f1a\u5171\u4eab\u540c\u4e00\u4e2a\u7c7b\u3002<br \/>\n3. \u6700\u540e\uff0c\u63d2\u5165\u4e00\u4e2a\u666e\u901a\u7684\u00a0<code>&lt;button&gt;<\/code>\u00a0\u6807\u7b7e\uff0c\u4f46\u6dfb\u52a0\u00a0<code>is=&quot;hello-button&quot;<\/code>\u00a0\u5230\u8fd9\u4e2a\u5143\u7d20\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4f7f\u7528\u6211\u4eec\u7684 custom element\uff1a<\/p>\n<pre><code class=\"language-js\">&lt;button is=&quot;hello-button&quot;&gt;...&lt;\/button&gt;\n<\/code><\/pre>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-js\">&lt;script&gt;\n\/\/ \u8fd9\u4e2a\u6309\u94ae\u5728\u88ab\u70b9\u51fb\u7684\u65f6\u5019\u8bf4 &quot;hello&quot;\nclass HelloButton extends HTMLButtonElement {\n  constructor() {\n    super();\n    this.addEventListener('click', () =&gt; alert(&quot;Hello!&quot;));\n  }\n}\n\ncustomElements.define('hello-button', HelloButton, {extends: 'button'});\n&lt;\/script&gt;\n\n&lt;button is=&quot;hello-button&quot;&gt;Click me&lt;\/button&gt;\n\n&lt;button is=&quot;hello-button&quot; disabled&gt;Disabled&lt;\/button&gt;\n<\/code><\/pre>\n<h2>MORE<\/h2>\n<ul>\n<li>HTML \u73b0\u884c\u6807\u51c6\uff1a\u00a0<a href=\"https:\/\/html.spec.whatwg.org\/#custom-elements\">https:\/\/html.spec.whatwg.org\/#custom-elements<\/a>\u3002<\/li>\n<li>\u517c\u5bb9\u6027\uff1a\u00a0<a href=\"https:\/\/caniuse.com\/#feat=custom-elements\">https:\/\/caniuse.com\/#feat=custom-elements<\/a>\u3002<\/li>\n<\/ul>\n<h1>\u5f71\u5b50 DOM (Shadow DOM)<\/h1>\n<p>Shadow DOM \u4e3a\u5c01\u88c5\u800c\u751f\u3002\u5b83\u53ef\u4ee5\u8ba9\u4e00\u4e2a\u7ec4\u4ef6\u62e5\u6709\u81ea\u5df1\u7684\u300c\u5f71\u5b50\u300dDOM \u6811\uff0c\u8fd9\u4e2a DOM \u6811\u4e0d\u80fd\u5728\u4e3b\u6587\u6863\u4e2d\u88ab\u4efb\u610f\u8bbf\u95ee\uff0c\u53ef\u80fd\u62e5\u6709\u5c40\u90e8\u6837\u5f0f\u89c4\u5219\uff0c\u8fd8\u6709\u5176\u4ed6\u7279\u6027\u3002<\/p>\n<h2>\u5185\u5efa shadow DOM<\/h2>\n<p>\u4f60\u662f\u5426\u66fe\u7ecf\u601d\u8003\u8fc7\u590d\u6742\u7684\u6d4f\u89c8\u5668\u63a7\u4ef6\u662f\u5982\u4f55\u88ab\u521b\u5efa\u548c\u6dfb\u52a0\u6837\u5f0f\u7684\uff1f<br \/>\n\u6bd4\u5982\u00a0<code>&lt;input type=&quot;range&quot;&gt;<\/code>\uff1a<br \/>\n\u6d4f\u89c8\u5668\u5728\u5185\u90e8\u4f7f\u7528 DOM\/CSS \u6765\u7ed8\u5236\u5b83\u4eec\u3002\u8fd9\u4e2a DOM \u7ed3\u6784\u4e00\u822c\u6765\u8bf4\u5bf9\u6211\u4eec\u662f\u9690\u85cf\u7684\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u5728\u5f00\u53d1\u8005\u5de5\u5177\u91cc\u9762\u770b\u89c1\u5b83\u3002\u6bd4\u5982\uff0c\u5728 Chrome \u91cc\uff0c\u6211\u4eec\u9700\u8981\u6253\u5f00\u300cShow user agent shadow DOM\u300d\u9009\u9879\u3002<br \/>\n\u7136\u540e\u00a0<code>&lt;input type=&quot;range&quot;&gt;<\/code>\u00a0\u770b\u8d77\u6765\u4f1a\u50cf\u8fd9\u6837\uff1a<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/levin.ink\/wp-content\/uploads\/2024\/07\/Pasted-image-20240704000144.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/levin.ink\/wp-content\/uploads\/2024\/07\/Pasted-image-20240704000144.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"Pasted image 20240704000144.png\"><\/div><br \/>\n\u4f60\u5728\u00a0<code>#shadow-root<\/code>\u00a0\u4e0b\u770b\u5230\u7684\u5c31\u662f\u88ab\u79f0\u4e3a\u300cshadow DOM\u300d\u7684\u4e1c\u897f\u3002<br \/>\n\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528\u4e00\u822c\u7684 JavaScript \u8c03\u7528\u6216\u8005\u9009\u62e9\u5668\u6765\u83b7\u53d6\u5185\u5efa shadow DOM \u5143\u7d20\u3002\u5b83\u4eec\u4e0d\u662f\u5e38\u89c4\u7684\u5b50\u5143\u7d20\uff0c\u800c\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5c01\u88c5\u624b\u6bb5\u3002<br \/>\n\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u6709\u7528\u7684\u5c5e\u6027\u00a0<code>pseudo<\/code>\u3002\u8fd9\u662f\u4e00\u4e2a\u56e0\u4e3a\u5386\u53f2\u539f\u56e0\u800c\u5b58\u5728\u7684\u5c5e\u6027\uff0c\u5e76\u4e0d\u5728\u6807\u51c6\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u7ed9\u5b50\u5143\u7d20\u52a0\u4e0a CSS \u6837\u5f0f\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;style&gt;\n\/* \u8ba9\u6ed1\u5757\u8f68\u9053\u53d8\u7ea2 *\/\ninput::-webkit-slider-runnable-track {\n  background: red;\n}\n&lt;\/style&gt;\n\n&lt;input type=&quot;range&quot;&gt;\n<\/code><\/pre>\n<p>\u91cd\u7533\u4e00\u6b21\uff0c<code>pseudo<\/code>\u00a0\u662f\u4e00\u4e2a\u975e\u6807\u51c6\u7684\u5c5e\u6027\u3002\u6309\u7167\u65f6\u95f4\u987a\u5e8f\u6765\u8bf4\uff0c\u6d4f\u89c8\u5668\u9996\u5148\u5b9e\u9a8c\u4e86\u4f7f\u7528\u5185\u90e8 DOM \u7ed3\u6784\u6765\u5b9e\u73b0\u63a7\u4ef6\uff0c\u7136\u540e\uff0c\u5728\u4e00\u6bb5\u65f6\u95f4\u4e4b\u540e\uff0cshadow DOM \u624d\u88ab\u6807\u51c6\u5316\u6765\u8ba9\u6211\u4eec\uff0c\u5f00\u53d1\u8005\u4eec\uff0c\u505a\u7c7b\u4f3c\u7684\u4e8b\u3002<br \/>\n\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8981\u4f7f\u7528\u73b0\u4ee3 shadow DOM \u6807\u51c6\uff0c\u5b83\u5728\u00a0<a href=\"https:\/\/dom.spec.whatwg.org\/#shadow-trees\">DOM spec<\/a>\u00a0\u548c\u5176\u4ed6\u76f8\u5173\u6807\u51c6\u4e2d\u53ef\u4ee5\u88ab\u627e\u5230\u3002<\/p>\n<h2>Shadow tree<\/h2>\n<p>\u4e00\u4e2a DOM \u5143\u7d20\u53ef\u4ee5\u6709\u4ee5\u4e0b\u4e24\u7c7b DOM \u5b50\u6811\uff1a<\/p>\n<ol>\n<li>Light tree\uff08\u5149\u660e\u6811\uff09 \u2014\u2014 \u4e00\u4e2a\u5e38\u89c4 DOM \u5b50\u6811\uff0c\u7531 HTML \u5b50\u5143\u7d20\u7ec4\u6210\u3002\u6211\u4eec\u5728\u4e4b\u524d\u7ae0\u8282\u770b\u5230\u7684\u6240\u6709\u5b50\u6811\u90fd\u662f\u300c\u5149\u660e\u7684\u300d\u3002<\/li>\n<li>Shadow tree\uff08\u5f71\u5b50\u6811\uff09 \u2014\u2014 \u4e00\u4e2a\u9690\u85cf\u7684 DOM \u5b50\u6811\uff0c\u4e0d\u5728 HTML \u4e2d\u53cd\u6620\uff0c\u65e0\u6cd5\u88ab\u5bdf\u89c9\u3002<br \/>\n\u5982\u679c\u4e00\u4e2a\u5143\u7d20\u540c\u65f6\u6709\u4ee5\u4e0a\u4e24\u79cd\u5b50\u6811\uff0c\u90a3\u4e48\u6d4f\u89c8\u5668\u53ea\u6e32\u67d3 shadow tree\u3002\u4f46\u662f\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u8bbe\u7f6e\u4e24\u79cd\u6811\u7684\u7ec4\u5408\u3002\u6211\u4eec\u5c06\u4f1a\u5728\u540e\u9762\u7684\u7ae0\u8282\u00a0<a href=\"https:\/\/zh.javascript.info\/slots-composition\">Shadow DOM \u63d2\u69fd\uff0c\u7ec4\u6210<\/a>\u00a0\u4e2d\u770b\u5230\u66f4\u591a\u7ec6\u8282\u3002<br \/>\n\u5f71\u5b50\u6811\u53ef\u4ee5\u5728\u81ea\u5b9a\u4e49\u5143\u7d20\u4e2d\u88ab\u4f7f\u7528\uff0c\u5176\u4f5c\u7528\u662f\u9690\u85cf\u7ec4\u4ef6\u5185\u90e8\u7ed3\u6784\u548c\u6dfb\u52a0\u53ea\u5728\u7ec4\u4ef6\u5185\u6709\u6548\u7684\u6837\u5f0f\u3002<br \/>\n\u6bd4\u5982\uff0c\u8fd9\u4e2a\u00a0<code>&lt;show-hello&gt;<\/code>\u00a0\u5143\u7d20\u5c06\u5b83\u7684\u5185\u90e8 DOM \u9690\u85cf\u5728\u4e86\u5f71\u5b50\u91cc\u9762\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-html\">&lt;script&gt;\ncustomElements.define('show-hello', class extends HTMLElement {\n  connectedCallback() {\n    const shadow = this.attachShadow({mode: 'open'});\n    shadow.innerHTML = `&lt;p&gt;\n      Hello, ${this.getAttribute('name')}\n    &lt;\/p&gt;`;\n  }\n});\n&lt;\/script&gt;\n\n&lt;show-hello name=&quot;John&quot;&gt;&lt;\/show-hello&gt;\n<\/code><\/pre>\n<p>\u9996\u5148\uff0c\u8c03\u7528\u00a0<code>elem.attachShadow({mode: \u2026})<\/code>\u00a0\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a shadow tree\u3002<br \/>\n\u8fd9\u91cc\u6709\u4e24\u4e2a\u9650\u5236\uff1a<\/p>\n<ol>\n<li>\u5728\u6bcf\u4e2a\u5143\u7d20\u4e2d\uff0c\u6211\u4eec\u53ea\u80fd\u521b\u5efa\u4e00\u4e2a shadow root\u3002<\/li>\n<li><code>elem<\/code>\u00a0\u5fc5\u987b\u662f\u81ea\u5b9a\u4e49\u5143\u7d20\uff0c\u6216\u8005\u662f\u4ee5\u4e0b\u5143\u7d20\u7684\u5176\u4e2d\u4e00\u4e2a\uff1a\u300carticle\u300d\u3001\u300caside\u300d\u3001\u300cblockquote\u300d\u3001\u300cbody\u300d\u3001\u300cdiv\u300d\u3001\u300cfooter\u300d\u3001\u300ch1\u2026h6\u300d\u3001\u300cheader\u300d\u3001\u300cmain\u300d\u3001\u300cnav\u300d\u3001\u300cp\u300d\u3001\u300csection\u300d\u6216\u8005\u300cspan\u300d\u3002\u5176\u4ed6\u5143\u7d20\uff0c\u6bd4\u5982\u00a0<code>&lt;img&gt;<\/code>\uff0c\u4e0d\u80fd\u5bb9\u7eb3 shadow tree\u3002<br \/>\n<code>mode<\/code>\u00a0\u9009\u9879\u53ef\u4ee5\u8bbe\u5b9a\u5c01\u88c5\u5c42\u7ea7\u3002\u4ed6\u5fc5\u987b\u662f\u4ee5\u4e0b\u4e24\u4e2a\u503c\u4e4b\u4e00\uff1a<\/li>\n<\/ol>\n<ul>\n<li><code>\u300copen\u300d<\/code>\u00a0\u2014\u2014 shadow root \u53ef\u4ee5\u901a\u8fc7\u00a0<code>elem.shadowRoot<\/code>\u00a0\u8bbf\u95ee\u3002<br \/>\n\u4efb\u4f55\u4ee3\u7801\u90fd\u53ef\u4ee5\u8bbf\u95ee\u00a0<code>elem<\/code>\u00a0\u7684 shadow tree\u3002<\/li>\n<li><code>\u300cclosed\u300d<\/code>\u00a0\u2014\u2014\u00a0<code>elem.shadowRoot<\/code>\u00a0\u6c38\u8fdc\u662f\u00a0<code>null<\/code>\u3002<br \/>\n\u6211\u4eec\u53ea\u80fd\u901a\u8fc7\u00a0<code>attachShadow<\/code>\u00a0\u8fd4\u56de\u7684\u6307\u9488\u6765\u8bbf\u95ee shadow DOM\uff08\u5e76\u4e14\u53ef\u80fd\u9690\u85cf\u5728\u4e00\u4e2a class \u4e2d\uff09\u3002\u6d4f\u89c8\u5668\u539f\u751f\u7684 shadow tree\uff0c\u6bd4\u5982\u00a0<code>&lt;input type=&quot;range&quot;&gt;<\/code>\uff0c\u662f\u5c01\u95ed\u7684\u3002\u6ca1\u6709\u4efb\u4f55\u65b9\u6cd5\u53ef\u4ee5\u8bbf\u95ee\u5b83\u4eec\u3002<br \/>\n<code>attachShadow<\/code>\u00a0\u8fd4\u56de\u7684\u00a0<a href=\"https:\/\/dom.spec.whatwg.org\/#shadowroot\">shadow root<\/a>\uff0c\u548c\u4efb\u4f55\u5143\u7d20\u4e00\u6837\uff1a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u00a0<code>innerHTML<\/code>\u00a0\u6216\u8005 DOM \u65b9\u6cd5\uff0c\u6bd4\u5982\u00a0<code>append<\/code>\u00a0\u6765\u6269\u5c55\u5b83\u3002<br \/>\n\u6211\u4eec\u79f0\u6709 shadow root \u7684\u5143\u7d20\u53eb\u505a\u300cshadow tree host\u300d\uff0c\u53ef\u4ee5\u901a\u8fc7 shadow root \u7684\u00a0<code>host<\/code>\u00a0\u5c5e\u6027\u8bbf\u95ee\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-js\">\/\/ \u5047\u8bbe {mode: &quot;open&quot;}\uff0c\u5426\u5219 elem.shadowRoot \u662f null\nalert(elem.shadowRoot.host === elem); \/\/ true\n<\/code><\/pre>\n<h2>\u5c01\u88c5<\/h2>\n<p>Shadow DOM \u88ab\u975e\u5e38\u660e\u663e\u5730\u548c\u4e3b\u6587\u6863\u5206\u5f00\uff1a<\/p>\n<ol>\n<li>Shadow DOM \u5143\u7d20\u5bf9\u4e8e light DOM \u4e2d\u7684\u00a0<code>querySelector<\/code>\u00a0\u4e0d\u53ef\u89c1\u3002\u5b9e\u9645\u4e0a\uff0cShadow DOM \u4e2d\u7684\u5143\u7d20\u53ef\u80fd\u4e0e light DOM \u4e2d\u67d0\u4e9b\u5143\u7d20\u7684 id \u51b2\u7a81\u3002\u8fd9\u4e9b\u5143\u7d20\u5fc5\u987b\u5728 shadow tree \u4e2d\u72ec\u4e00\u65e0\u4e8c\u3002<\/li>\n<li>Shadow DOM \u6709\u81ea\u5df1\u7684\u6837\u5f0f\u3002\u5916\u90e8\u6837\u5f0f\u89c4\u5219\u5728 shadow DOM \u4e2d\u4e0d\u4ea7\u751f\u4f5c\u7528\u3002<br \/>\n\u6bd4\u5982\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-html\">&lt;style&gt;\n  \/* \u6587\u6863\u6837\u5f0f\u5bf9 #elem \u5185\u7684 shadow tree \u65e0\u4f5c\u7528 (1) *\/\n  p { color: red; }\n&lt;\/style&gt;\n\n&lt;div id=&quot;elem&quot;&gt;&lt;\/div&gt;\n\n&lt;script&gt;\n  elem.attachShadow({mode: 'open'});\n    \/\/ shadow tree \u6709\u81ea\u5df1\u7684\u6837\u5f0f (2)\n  elem.shadowRoot.innerHTML = `\n    &lt;style&gt; p { font-weight: bold; } &lt;\/style&gt;\n    &lt;p&gt;Hello, John!&lt;\/p&gt;\n  `;\n\n  \/\/ &lt;p&gt; \u53ea\u5bf9 shadow tree \u91cc\u9762\u7684\u67e5\u8be2\u53ef\u89c1 (3)\n  alert(document.querySelectorAll('p').length); \/\/ 0\n  alert(elem.shadowRoot.querySelectorAll('p').length); \/\/ 1\n&lt;\/script&gt;\n<\/code><\/pre>\n<ol>\n<li>\u6587\u6863\u91cc\u9762\u7684\u6837\u5f0f\u5bf9 shadow tree \u6ca1\u6709\u4efb\u4f55\u6548\u679c\u3002<\/li>\n<li>\u2026\u2026\u4f46\u662f\u5185\u90e8\u7684\u6837\u5f0f\u662f\u6709\u6548\u7684\u3002<\/li>\n<li>\u4e3a\u4e86\u83b7\u53d6 shadow tree \u5185\u90e8\u7684\u5143\u7d20\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u6811\u7684\u5185\u90e8\u67e5\u8be2\u3002<\/li>\n<\/ol>\n<h2>MORE<\/h2>\n<ul>\n<li>DOM\uff1a<a href=\"https:\/\/dom.spec.whatwg.org\/#shadow-trees\">https:\/\/dom.spec.whatwg.org\/#shadow-trees<\/a><\/li>\n<li>\u517c\u5bb9\u6027\uff1a<a href=\"https:\/\/caniuse.com\/#feat=shadowdomv1\">https:\/\/caniuse.com\/#feat=shadowdomv1<\/a><\/li>\n<li>Shadow DOM \u5728\u5f88\u591a\u5176\u4ed6\u6807\u51c6\u4e2d\u88ab\u63d0\u5230\uff0c\u6bd4\u5982\uff1a<a href=\"https:\/\/w3c.github.io\/DOM-Parsing\/#the-innerhtml-mixin\">DOM Parsing<\/a>\u00a0\u6307\u5b9a\u4e86 shadow root \u6709\u00a0<code>innerHTML<\/code>\u3002<\/li>\n<\/ul>\n<h1>\u6a21\u677f\u5143\u7d20<\/h1>\n<p>\u5185\u5efa\u7684\u00a0<code>&lt;template&gt;<\/code>\u00a0\u5143\u7d20\u7528\u6765\u5b58\u50a8 HTML \u6a21\u677f\u3002\u6d4f\u89c8\u5668\u5c06\u5ffd\u7565\u5b83\u7684\u5185\u5bb9\uff0c\u4ec5\u68c0\u67e5\u8bed\u6cd5\u7684\u6709\u6548\u6027\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u5728 JavaScript \u4e2d\u8bbf\u95ee\u548c\u4f7f\u7528\u5b83\u6765\u521b\u5efa\u5176\u4ed6\u5143\u7d20\u3002<br \/>\n\u4ece\u7406\u8bba\u4e0a\u8bb2\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 HTML \u4e2d\u7684\u4efb\u4f55\u4f4d\u7f6e\u521b\u5efa\u4e0d\u53ef\u89c1\u5143\u7d20\u6765\u50a8\u5b58 HTML \u6a21\u677f\u3002\u90a3\u00a0<code>&lt;template&gt;<\/code>\u00a0\u5143\u7d20\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<br \/>\n\u9996\u5148\uff0c\u5176\u5185\u5bb9\u53ef\u4ee5\u662f\u4efb\u4f55\u6709\u6548\u7684HTML\uff0c\u5373\u4f7f\u5b83\u901a\u5e38\u9700\u8981\u7279\u5b9a\u7684\u5c01\u95ed\u6807\u7b7e\u3002<br \/>\n\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5176\u4e2d\u653e\u7f6e\u4e00\u884c\u8868\u683c\u00a0<code>&lt;tr&gt;<\/code>\u00a0\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;template&gt;\n  &lt;tr&gt;\n    &lt;td&gt;Contents&lt;\/td&gt;\n  &lt;\/tr&gt;\n&lt;\/template&gt;\n<\/code><\/pre>\n<p>\u901a\u5e38\uff0c\u5982\u679c\u6211\u4eec\u5728\u00a0<code>&lt;tr&gt;<\/code>\u00a0\u5185\u653e\u7f6e\u7c7b\u4f3c\u00a0<code>&lt;div&gt;<\/code>\u00a0\u7684\u5143\u7d20\uff0c\u6d4f\u89c8\u5668\u4f1a\u68c0\u6d4b\u5230\u65e0\u6548\u7684 DOM \u7ed3\u6784\u5e76\u5bf9\u5176\u8fdb\u884c\u201c\u4fee\u590d\u201d\uff0c\u7136\u540e\u7528\u00a0<code>&lt;table&gt;<\/code>\u00a0\u5c01\u95ed\u00a0<code>&lt;tr&gt;<\/code>\u00a0\uff0c\u90a3\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002\u800c\u00a0<code>&lt;template&gt;<\/code>\u00a0\u5219\u5b8c\u5168\u4fdd\u7559\u6211\u4eec\u50a8\u5b58\u7684\u5185\u5bb9\u3002<br \/>\n\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06\u6837\u5f0f\u548c\u811a\u672c\u653e\u5165\u00a0<code>&lt;template&gt;<\/code>\u00a0\u5143\u7d20\u4e2d\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;template&gt;\n  &lt;style&gt;\n    p { font-weight: bold; }\n  &lt;\/style&gt;\n  &lt;script&gt;\n    alert(&quot;Hello&quot;);\n  &lt;\/script&gt;\n&lt;\/template&gt;\n<\/code><\/pre>\n<p>\u6d4f\u89c8\u5668\u8ba4\u4e3a\u00a0<code>&lt;template&gt;<\/code>\u00a0\u7684\u5185\u5bb9\u201c\u4e0d\u5728\u6587\u6863\u4e2d\u201d\uff1a\u6837\u5f0f\u4e0d\u4f1a\u88ab\u5e94\u7528\uff0c\u811a\u672c\u4e5f\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u00a0<code>&lt;video autoplay&gt;<\/code>\u00a0\u4e5f\u4e0d\u4f1a\u8fd0\u884c\uff0c\u7b49\u3002<br \/>\n\u5f53\u6211\u4eec\u5c06\u5185\u5bb9\u63d2\u5165\u6587\u6863\u65f6\uff0c\u8be5\u5185\u5bb9\u5c06\u53d8\u4e3a\u6d3b\u52a8\u72b6\u6001\uff08\u5e94\u7528\u6837\u5f0f\uff0c\u8fd0\u884c\u811a\u672c\u7b49\uff09\u3002<\/p>\n<h2>\u63d2\u5165\u6a21\u677f<\/h2>\n<p>\u6a21\u677f\u7684\u00a0<code>content<\/code>\u00a0\u5c5e\u6027\u53ef\u770b\u4f5c<a href=\"https:\/\/zh.javascript.info\/modifying-document#document-fragment\">DocumentFragment<\/a>\u00a0\u2014\u2014 \u4e00\u79cd\u7279\u6b8a\u7684 DOM \u8282\u70b9\u3002<br \/>\n\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u89c6\u4e3a\u666e\u901a\u7684DOM\u8282\u70b9\uff0c\u9664\u4e86\u5b83\u6709\u4e00\u4e2a\u7279\u6b8a\u5c5e\u6027\uff1a\u5c06\u5176\u63d2\u5165\u67d0\u4e2a\u4f4d\u7f6e\u65f6\uff0c\u4f1a\u88ab\u63d2\u5165\u7684\u5219\u662f\u5176\u5b50\u8282\u70b9\u3002<br \/>\n\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;template id=&quot;tmpl&quot;&gt;\n  &lt;script&gt;\n    alert(&quot;Hello&quot;);\n  &lt;\/script&gt;\n  &lt;div class=&quot;message&quot;&gt;Hello, world!&lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\n  let elem = document.createElement('div');\n\n  \/\/ Clone the template content to reuse it multiple times\n  elem.append(tmpl.content.cloneNode(true));\n\n  document.body.append(elem);\n  \/\/ Now the script from &lt;template&gt; runs\n&lt;\/script&gt;\n<\/code><\/pre>\n<p>\u8ba9\u6211\u4eec\u7528\u00a0<code>&lt;template&gt;<\/code>\u00a0\u91cd\u5199\u4e0a\u4e00\u7ae0\u7684 Shadow DOM \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;template id=&quot;tmpl&quot;&gt;\n  &lt;style&gt; p { font-weight: bold; } &lt;\/style&gt;\n  &lt;p id=&quot;message&quot;&gt;&lt;\/p&gt;\n&lt;\/template&gt;\n\n&lt;div id=&quot;elem&quot;&gt;Click me&lt;\/div&gt;\n\n&lt;script&gt;\n  elem.onclick = function() {\n    elem.attachShadow({mode: 'open'});\n\n    elem.shadowRoot.append(tmpl.content.cloneNode(true)); \/\/ (*)\n\n    elem.shadowRoot.getElementById('message').innerHTML = &quot;Hello from the shadows!&quot;;\n  };\n&lt;\/script&gt;\n<\/code><\/pre>\n<p>\u5728\u00a0<code>(*)<\/code>\u00a0\u884c\uff0c\u6211\u4eec\u5c06\u00a0<code>tmpl.content<\/code>\u00a0\u4f5c\u4e3a\u00a0<code>DocumentFragment<\/code>\u00a0\u514b\u9686\u548c\u63d2\u5165\uff0c\u5b83\u7684\u5b50\u8282\u70b9\uff08<code>&lt;style&gt;<\/code>\uff0c<code>&lt;p&gt;<\/code>\uff09\u5c06\u4ee3\u4e3a\u63d2\u5165\u3002<br \/>\n\u5b83\u4eec\u4f1a\u53d8\u6210\u4e00\u4e2a Shadow DOM\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;div id=&quot;elem&quot;&gt;\n  #shadow-root\n    &lt;style&gt; p { font-weight: bold; } &lt;\/style&gt;\n    &lt;p id=&quot;message&quot;&gt;&lt;\/p&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<h1>Shadow DOM \u63d2\u69fd\uff0c\u7ec4\u6210<\/h1>\n<p>\u8bb8\u591a\u7c7b\u578b\u7684\u7ec4\u4ef6\uff0c\u4f8b\u5982\u6807\u7b7e\u3001\u83dc\u5355\u3001\u7167\u7247\u5e93\u7b49\u7b49\uff0c\u9700\u8981\u5185\u5bb9\u53bb\u6e32\u67d3\u3002<br \/>\n\u5c31\u50cf\u6d4f\u89c8\u5668\u5185\u5efa\u7684\u00a0<code>&lt;select&gt;<\/code>\u00a0\u9700\u8981\u00a0<code>&lt;option&gt;<\/code>\u00a0\u5b50\u9879\uff0c\u6211\u4eec\u7684\u00a0<code>&lt;custom-tabs&gt;<\/code>\u00a0\u53ef\u80fd\u9700\u8981\u5b9e\u9645\u7684\u6807\u7b7e\u5185\u5bb9\u6765\u8d77\u4f5c\u7528\u3002\u5e76\u4e14\u4e00\u4e2a\u00a0<code>&lt;custom-menu&gt;<\/code>\u00a0\u53ef\u80fd\u9700\u8981\u83dc\u5355\u5b50\u9879\u3002<br \/>\n\u4f7f\u7528\u4e86\u00a0<code>&lt;custom-menu&gt;<\/code>\u00a0\u7684\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;custom-menu&gt;\n  &lt;title&gt;Candy menu&lt;\/title&gt;\n  &lt;item&gt;Lollipop&lt;\/item&gt;\n  &lt;item&gt;Fruit Toast&lt;\/item&gt;\n  &lt;item&gt;Cup Cake&lt;\/item&gt;\n&lt;\/custom-menu&gt;\n<\/code><\/pre>\n<p>\u2026\u2026\u4e4b\u540e\uff0c\u6211\u4eec\u7684\u7ec4\u4ef6\u5e94\u8be5\u6b63\u786e\u5730\u6e32\u67d3\u6210\u5177\u6709\u7ed9\u5b9a\u6807\u9898\u548c\u9879\u76ee\u3001\u5904\u7406\u83dc\u5355\u4e8b\u4ef6\u7b49\u7684\u6f02\u4eae\u83dc\u5355\u3002<br \/>\n\u5982\u4f55\u5b9e\u73b0\u5462\uff1f<br \/>\n\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u5206\u6790\u5143\u7d20\u5185\u5bb9\u5e76\u52a8\u6001\u590d\u5236\u91cd\u65b0\u6392\u5217 DOM \u8282\u70b9\u3002\u8fd9\u662f\u53ef\u80fd\u7684\uff0c\u4f46\u662f\u5982\u679c\u6211\u4eec\u8981\u5c06\u5143\u7d20\u79fb\u52a8\u5230 Shadow DOM\uff0c\u90a3\u4e48\u6587\u6863\u7684 CSS \u6837\u5f0f\u4e0d\u80fd\u5728\u90a3\u91cc\u5e94\u7528\uff0c\u56e0\u6b64\u6587\u6863\u7684\u89c6\u89c9\u6837\u5f0f\u53ef\u80fd\u4f1a\u4e22\u5931\u3002\u770b\u8d77\u6765\u8fd8\u9700\u8981\u505a\u4e00\u4e9b\u4e8b\u60c5\u3002<br \/>\n\u5e78\u8fd0\u7684\u662f\u6211\u4eec\u4e0d\u9700\u8981\u53bb\u505a\u3002Shadow DOM \u652f\u6301\u00a0<code>&lt;slot&gt;<\/code>\u00a0\u5143\u7d20\uff0c\u7531 light DOM \u4e2d\u7684\u5185\u5bb9\u81ea\u52a8\u586b\u5145\u3002<\/p>\n<h2>\u5177\u540d\u63d2\u69fd<\/h2>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u770b\u4e0b\u63d2\u69fd\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002<br \/>\n\u5728\u8fd9\u91cc\u00a0<code>&lt;user-card&gt;<\/code>\u00a0shadow DOM \u63d0\u4f9b\u4e24\u4e2a\u63d2\u69fd, \u4ece light DOM \u586b\u5145\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;script&gt;\ncustomElements.define('user-card', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.innerHTML = `\n      &lt;div&gt;Name:\n        &lt;slot name=&quot;username&quot;&gt;&lt;\/slot&gt;\n      &lt;\/div&gt;\n      &lt;div&gt;Birthday:\n        &lt;slot name=&quot;birthday&quot;&gt;&lt;\/slot&gt;\n      &lt;\/div&gt;\n    `;\n  }\n});\n&lt;\/script&gt;\n\n&lt;user-card&gt;\n  &lt;span slot=&quot;username&quot;&gt;John Smith&lt;\/span&gt;\n  &lt;span slot=&quot;birthday&quot;&gt;01.01.2001&lt;\/span&gt;\n&lt;\/user-card&gt;\n<\/code><\/pre>\n<p>\u5728 shadow DOM \u4e2d\uff0c<code>&lt;slot name=&quot;X&quot;&gt;<\/code>\u00a0\u5b9a\u4e49\u4e86\u4e00\u4e2a\u201c\u63d2\u5165\u70b9\u201d\uff0c\u4e00\u4e2a\u5e26\u6709\u00a0<code>slot=&quot;X&quot;<\/code>\u00a0\u7684\u5143\u7d20\u88ab\u6e32\u67d3\u7684\u5730\u65b9\u3002<br \/>\n\u7136\u540e\u6d4f\u89c8\u5668\u6267\u884c\u201d\u7ec4\u5408\u201c\uff1a\u5b83\u4ece light DOM \u4e2d\u83b7\u53d6\u5143\u7d20\u5e76\u4e14\u6e32\u67d3\u5230 shadow DOM \u4e2d\u7684\u5bf9\u5e94\u63d2\u69fd\u4e2d\u3002\u6700\u540e\uff0c\u6b63\u662f\u6211\u4eec\u60f3\u8981\u7684 \u2014\u2014 \u4e00\u4e2a\u80fd\u88ab\u586b\u5145\u6570\u636e\u7684\u901a\u7528\u7ec4\u4ef6\u3002<br \/>\n\u8fd9\u662f\u7f16\u8bd1\u540e\uff0c\u4e0d\u8003\u8651\u7ec4\u5408\u7684 DOM \u7ed3\u6784\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;user-card&gt;\n  #shadow-root\n    &lt;div&gt;Name:\n      &lt;slot name=&quot;username&quot;&gt;&lt;\/slot&gt;\n    &lt;\/div&gt;\n    &lt;div&gt;Birthday:\n      &lt;slot name=&quot;birthday&quot;&gt;&lt;\/slot&gt;\n    &lt;\/div&gt;\n  &lt;span slot=&quot;username&quot;&gt;John Smith&lt;\/span&gt;\n  &lt;span slot=&quot;birthday&quot;&gt;01.01.2001&lt;\/span&gt;\n&lt;\/user-card&gt;\n<\/code><\/pre>\n<p>\u6211\u4eec\u521b\u5efa\u4e86 shadow DOM\uff0c\u6240\u4ee5\u5b83\u5f53\u7136\u5c31\u5b58\u5728\u4e86\uff0c\u4f4d\u4e8e\u00a0<code>#shadow-root<\/code>\u00a0\u4e4b\u4e0b\u3002\u73b0\u5728\u5143\u7d20\u540c\u65f6\u62e5\u6709 light DOM \u548c shadow DOM\u3002<br \/>\n\u4e3a\u4e86\u6e32\u67d3 shadow DOM \u4e2d\u7684\u6bcf\u4e00\u4e2a\u00a0<code>&lt;slot name=&quot;...&quot;&gt;<\/code>\u00a0\u5143\u7d20\uff0c\u6d4f\u89c8\u5668\u5728 light DOM \u4e2d\u5bfb\u627e\u76f8\u540c\u540d\u5b57\u7684\u00a0<code>slot=&quot;...&quot;<\/code>\u3002\u8fd9\u4e9b\u5143\u7d20\u5728\u63d2\u69fd\u5185\u88ab\u6e32\u67d3\uff1a<br \/>\n<div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/levin.ink\/wp-content\/uploads\/2024\/07\/Pasted-image-20240704193553.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/levin.ink\/wp-content\/uploads\/2024\/07\/Pasted-image-20240704193553.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"Pasted image 20240704193553.png\"><\/div><br \/>\n\u7ed3\u679c\u88ab\u53eb\u505a\u6241\u5e73\u5316\uff08flattened\uff09DOM\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;user-card&gt;\n  #shadow-root\n    &lt;div&gt;Name:\n      &lt;slot name=&quot;username&quot;&gt;\n        &lt;!-- slotted element is inserted into the slot --&gt;\n        &lt;span slot=&quot;username&quot;&gt;John Smith&lt;\/span&gt;\n      &lt;\/slot&gt;\n    &lt;\/div&gt;\n    &lt;div&gt;Birthday:\n      &lt;slot name=&quot;birthday&quot;&gt;\n        &lt;span slot=&quot;birthday&quot;&gt;01.01.2001&lt;\/span&gt;\n      &lt;\/slot&gt;\n    &lt;\/div&gt;\n&lt;\/user-card&gt;\n<\/code><\/pre>\n<p>\u2026\u2026\u4f46\u662f \u201cflattened\u201d DOM \u4ec5\u4ec5\u88ab\u521b\u5efa\u7528\u6765\u6e32\u67d3\u548c\u4e8b\u4ef6\u5904\u7406\uff0c\u662f\u201c\u865a\u62df\u201d\u7684\u3002\u867d\u7136\u662f\u6e32\u67d3\u51fa\u6765\u4e86\uff0c\u4f46\u6587\u6863\u4e2d\u7684\u8282\u70b9\u4e8b\u5b9e\u4e0a\u5e76\u6ca1\u6709\u5230\u5904\u79fb\u52a8\uff01<br \/>\n\u5982\u679c\u6211\u4eec\u8c03\u7528\u00a0<code>querySelectorAll<\/code>\u00a0\u90a3\u5c31\u5f88\u5bb9\u6613\u9a8c\u8bc1\uff1a\u8282\u70b9\u4ecd\u5728\u5b83\u4eec\u7684\u4f4d\u7f6e\u3002<\/p>\n<pre><code class=\"language-js\">\/\/ light DOM &lt;span&gt; \u8282\u70b9\u4f4d\u7f6e\u4f9d\u7136\u4e0d\u53d8\uff0c\u5728 `&lt;user-card&gt;` \u91cc\nalert( document.querySelectorAll('user-card span').length ); \/\/ 2\n<\/code><\/pre>\n<p>\u56e0\u6b64\uff0c\u6241\u5e73\u5316 DOM \u662f\u901a\u8fc7\u63d2\u5165\u63d2\u69fd\u4ece shadow DOM \u6d3e\u751f\u51fa\u6765\u7684\u3002\u6d4f\u89c8\u5668\u6e32\u67d3\u5b83\u5e76\u4e14\u7528\u4e8e\u6837\u5f0f\u7ee7\u627f\u3001\u4e8b\u4ef6\u4f20\u64ad\u3002\u4f46\u662f JavaScript \u5728\u6241\u5e73\u524d\u4ecd\u6309\u539f\u6837\u770b\u5230\u6587\u6863\u3002<\/p>\n<blockquote>\n<p>\u4ec5\u9876\u5c42\u5b50\u5143\u7d20\u53ef\u4ee5\u8bbe\u7f6e slot=&quot;\u2026&quot; \u7279\u6027<br \/>\n<code>slot=&quot;...&quot;<\/code>\u00a0\u5c5e\u6027\u4ec5\u4ec5\u5bf9 shadow host \u7684\u76f4\u63a5\u5b50\u4ee3 (\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\u7684\u00a0<code>&lt;user-card&gt;<\/code>\u00a0\u5143\u7d20) \u6709\u6548\u3002\u5bf9\u4e8e\u5d4c\u5957\u5143\u7d20\u5b83\u5c06\u88ab\u5ffd\u7565\u3002<br \/>\n\u4f8b\u5982\uff0c\u8fd9\u91cc\u7684\u7b2c\u4e8c\u4e2a\u00a0<code>&lt;span&gt;<\/code>\u00a0\u88ab\u5ffd\u7565\u4e86(\u56e0\u4e3a\u5b83\u4e0d\u662f\u00a0<code>&lt;user-card&gt;<\/code>\u00a0\u7684\u9876\u5c42\u5b50\u5143\u7d20)\uff1a<\/p>\n<\/blockquote>\n<pre><code class=\"language-html\">&lt;user-card&gt;\n  &lt;span slot=&quot;username&quot;&gt;John Smith&lt;\/span&gt;\n  &lt;div&gt;\n    &lt;!-- invalid slot, must be direct child of user-card --&gt;\n    &lt;span slot=&quot;birthday&quot;&gt;01.01.2001&lt;\/span&gt;\n  &lt;\/div&gt;\n&lt;\/user-card&gt;\n<\/code><\/pre>\n<p>\u5982\u679c\u5728 light DOM \u91cc\u6709\u591a\u4e2a\u76f8\u540c\u63d2\u69fd\u540d\u7684\u5143\u7d20\uff0c\u90a3\u4e48\u5b83\u4eec\u4f1a\u88ab\u4e00\u4e2a\u63a5\u4e00\u4e2a\u5730\u6dfb\u52a0\u5230\u63d2\u69fd\u4e2d\u3002<br \/>\n\u4f8b\u5982\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;user-card&gt;\n  &lt;span slot=&quot;username&quot;&gt;John&lt;\/span&gt;\n  &lt;span slot=&quot;username&quot;&gt;Smith&lt;\/span&gt;\n&lt;\/user-card&gt;\n<\/code><\/pre>\n<p>\u7ed9\u8fd9\u4e2a\u6241\u5e73\u5316 DOM \u4e24\u4e2a\u5143\u7d20\uff0c\u63d2\u5165\u5230\u00a0<code>&lt;slot name=&quot;username&quot;&gt;<\/code>\u00a0\u91cc\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;user-card&gt;\n  #shadow-root\n    &lt;div&gt;Name:\n      &lt;slot name=&quot;username&quot;&gt;\n        &lt;span slot=&quot;username&quot;&gt;John&lt;\/span&gt;\n        &lt;span slot=&quot;username&quot;&gt;Smith&lt;\/span&gt;\n      &lt;\/slot&gt;\n    &lt;\/div&gt;\n    &lt;div&gt;Birthday:\n      &lt;slot name=&quot;birthday&quot;&gt;&lt;\/slot&gt;\n    &lt;\/div&gt;\n&lt;\/user-card&gt;\n<\/code><\/pre>\n<h2>\u63d2\u69fd\u540e\u5907\u5185\u5bb9<\/h2>\n<p>\u5982\u679c\u6211\u4eec\u5728\u4e00\u4e2a\u00a0<code>&lt;slot&gt;<\/code>\u00a0\u5185\u90e8\u653e\u70b9\u4ec0\u4e48\uff0c\u5b83\u5c06\u6210\u4e3a\u540e\u5907\u5185\u5bb9\u3002\u5982\u679c light DOM \u4e2d\u6ca1\u6709\u76f8\u5e94\u586b\u5145\u7269\u7684\u8bdd\u6d4f\u89c8\u5668\u5c31\u5c55\u793a\u5b83\u3002<br \/>\n\u4f8b\u5982\uff0c\u5728\u8fd9\u91cc\u7684 shadow DOM \u4e2d\uff0c\u5982\u679c light DOM \u4e2d\u6ca1\u6709\u00a0<code>slot=&quot;username&quot;<\/code>\u00a0\u7684\u8bdd\u00a0<code>Anonymous<\/code>\u00a0\u5c31\u88ab\u6e32\u67d3\u3002<\/p>\n<pre><code class=\"language-html\">&lt;div&gt;Name:\n  &lt;slot name=&quot;username&quot;&gt;Anonymous&lt;\/slot&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<h2>\u9ed8\u8ba4\u63d2\u69fd\uff1a\u7b2c\u4e00\u4e2a\u4e0d\u5177\u540d\u7684\u63d2\u69fd<\/h2>\n<p>shadow DOM \u4e2d\u7b2c\u4e00\u4e2a\u6ca1\u6709\u540d\u5b57\u7684\u00a0<code>&lt;slot&gt;<\/code>\u00a0\u662f\u4e00\u4e2a\u9ed8\u8ba4\u63d2\u69fd\u3002\u5b83\u4ece light DOM \u4e2d\u83b7\u53d6\u6ca1\u6709\u653e\u7f6e\u5728\u5176\u4ed6\u4f4d\u7f6e\u7684\u6240\u6709\u8282\u70b9\u3002<br \/>\n\u4f8b\u5982\uff0c\u8ba9\u6211\u4eec\u628a\u9ed8\u8ba4\u63d2\u69fd\u6dfb\u52a0\u5230\u00a0<code>&lt;user-card&gt;<\/code>\uff0c\u8be5\u4f4d\u7f6e\u53ef\u4ee5\u6536\u96c6\u6709\u5173\u7528\u6237\u7684\u6240\u6709\u672a\u5f00\u69fd\uff08unslotted\uff09\u7684\u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;script&gt;\ncustomElements.define('user-card', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.innerHTML = `\n    &lt;div&gt;Name:\n      &lt;slot name=&quot;username&quot;&gt;&lt;\/slot&gt;\n    &lt;\/div&gt;\n    &lt;div&gt;Birthday:\n      &lt;slot name=&quot;birthday&quot;&gt;&lt;\/slot&gt;\n    &lt;\/div&gt;\n    &lt;fieldset&gt;\n      &lt;legend&gt;Other information&lt;\/legend&gt;\n      &lt;slot&gt;&lt;\/slot&gt;\n    &lt;\/fieldset&gt;\n    `;\n  }\n});\n&lt;\/script&gt;\n\n&lt;user-card&gt;\n  &lt;div&gt;I like to swim.&lt;\/div&gt;\n  &lt;span slot=&quot;username&quot;&gt;John Smith&lt;\/span&gt;\n  &lt;span slot=&quot;birthday&quot;&gt;01.01.2001&lt;\/span&gt;\n  &lt;div&gt;...And play volleyball too!&lt;\/div&gt;\n&lt;\/user-card&gt;\n<\/code><\/pre>\n<p>\u6240\u6709\u672a\u88ab\u63d2\u5165\u7684 light DOM \u5185\u5bb9\u8fdb\u5165 \u201c\u5176\u4ed6\u4fe1\u606f\u201d \u5b57\u6bb5\u96c6\u3002<br \/>\n\u5143\u7d20\u4e00\u4e2a\u63a5\u4e00\u4e2a\u7684\u9644\u52a0\u5230\u63d2\u69fd\u4e2d\uff0c\u56e0\u6b64\u8fd9\u4e24\u4e2a\u672a\u63d2\u5165\u63d2\u69fd\u7684\u4fe1\u606f\u90fd\u5728\u9ed8\u8ba4\u63d2\u69fd\u4e2d\u3002<br \/>\n\u6241\u5e73\u5316\u7684 DOM \u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;user-card&gt;\n  #shadow-root\n    &lt;div&gt;Name:\n      &lt;slot name=&quot;username&quot;&gt;\n        &lt;span slot=&quot;username&quot;&gt;John Smith&lt;\/span&gt;\n      &lt;\/slot&gt;\n    &lt;\/div&gt;\n    &lt;div&gt;Birthday:\n      &lt;slot name=&quot;birthday&quot;&gt;\n        &lt;span slot=&quot;birthday&quot;&gt;01.01.2001&lt;\/span&gt;\n      &lt;\/slot&gt;\n    &lt;\/div&gt;\n    &lt;fieldset&gt;\n      &lt;legend&gt;About me&lt;\/legend&gt;\n      &lt;slot&gt;\n        &lt;div&gt;Hello&lt;\/div&gt;\n        &lt;div&gt;I am John!&lt;\/div&gt;\n      &lt;\/slot&gt;\n    &lt;\/fieldset&gt;\n&lt;\/user-card&gt;\n<\/code><\/pre>\n<h2>Menu example<\/h2>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u56de\u5230\u5728\u672c\u7ae0\u5f00\u5934\u63d0\u5230\u7684\u00a0<code>&lt;custom-menu&gt;<\/code>\u00a0\u3002<br \/>\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u63d2\u69fd\u6765\u5206\u914d\u5143\u7d20\u3002<br \/>\n\u8fd9\u662f\u00a0<code>&lt;custom-menu&gt;<\/code>\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;custom-menu&gt;\n  &lt;span slot=&quot;title&quot;&gt;Candy menu&lt;\/span&gt;\n  &lt;li slot=&quot;item&quot;&gt;Lollipop&lt;\/li&gt;\n  &lt;li slot=&quot;item&quot;&gt;Fruit Toast&lt;\/li&gt;\n  &lt;li slot=&quot;item&quot;&gt;Cup Cake&lt;\/li&gt;\n&lt;\/custom-menu&gt;\n<\/code><\/pre>\n<p>\u5e26\u6709\u9002\u5f53\u63d2\u69fd\u7684 shadow DOM \u6a21\u7248\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;template id=&quot;tmpl&quot;&gt;\n  &lt;style&gt; \/* menu styles *\/ &lt;\/style&gt;\n  &lt;div class=&quot;menu&quot;&gt;\n    &lt;slot name=&quot;title&quot;&gt;&lt;\/slot&gt;\n    &lt;ul&gt;&lt;slot name=&quot;item&quot;&gt;&lt;\/slot&gt;&lt;\/ul&gt;\n  &lt;\/div&gt;\n&lt;\/template&gt;\n<\/code><\/pre>\n<ol>\n<li><code>&lt;span slot=&quot;title&quot;&gt;<\/code>\u00a0\u8fdb\u5165\u00a0<code>&lt;slot name=&quot;title&quot;&gt;<\/code>\u3002<\/li>\n<li>\u6a21\u7248\u4e2d\u6709\u8bb8\u591a\u00a0<code>&lt;li slot=&quot;item&quot;&gt;<\/code>\uff0c\u4f46\u662f\u53ea\u6709\u4e00\u4e2a\u00a0<code>&lt;slot name=&quot;item&quot;&gt;<\/code>\u3002\u56e0\u6b64\u6240\u6709\u5e26\u6709\u00a0<code>slot=&quot;item&quot;<\/code>\u00a0\u7684\u5143\u7d20\u90fd\u4e00\u4e2a\u63a5\u4e00\u4e2a\u5730\u9644\u52a0\u5230\u00a0<code>&lt;slot name=&quot;item&quot;&gt;<\/code>\u00a0\u4e0a\uff0c\u4ece\u800c\u5f62\u6210\u5217\u8868\u3002<br \/>\n\u6241\u5e73\u5316\u7684 DOM \u53d8\u4e3a\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-html\">&lt;custom-menu&gt;\n  #shadow-root\n    &lt;style&gt; \/* menu styles *\/ &lt;\/style&gt;\n    &lt;div class=&quot;menu&quot;&gt;\n      &lt;slot name=&quot;title&quot;&gt;\n        &lt;span slot=&quot;title&quot;&gt;Candy menu&lt;\/span&gt;\n      &lt;\/slot&gt;\n      &lt;ul&gt;\n        &lt;slot name=&quot;item&quot;&gt;\n          &lt;li slot=&quot;item&quot;&gt;Lollipop&lt;\/li&gt;\n          &lt;li slot=&quot;item&quot;&gt;Fruit Toast&lt;\/li&gt;\n          &lt;li slot=&quot;item&quot;&gt;Cup Cake&lt;\/li&gt;\n        &lt;\/slot&gt;\n      &lt;\/ul&gt;\n    &lt;\/div&gt;\n&lt;\/custom-menu&gt;\n<\/code><\/pre>\n<p>\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\uff0c\u5728\u6709\u6548\u7684 DOM \u4e2d\uff0c<code>&lt;li&gt;<\/code>\u00a0\u5fc5\u987b\u662f\u00a0<code>&lt;ul&gt;<\/code>\u00a0\u7684\u76f4\u63a5\u5b50\u4ee3\u3002\u4f46\u8fd9\u662f\u6241\u5e73\u5316\u7684 DOM\uff0c\u5b83\u63cf\u8ff0\u4e86\u7ec4\u4ef6\u7684\u6e32\u67d3\u65b9\u5f0f\uff0c\u8fd9\u6837\u7684\u4e8b\u60c5\u5728\u8fd9\u91cc\u81ea\u7136\u53d1\u751f\u3002<br \/>\n\u6211\u4eec\u53ea\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a\u00a0<code>click<\/code>\u00a0\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u6765\u6253\u5f00\/\u5173\u95ed\u5217\u8868\uff0c\u5e76\u4e14\u00a0<code>&lt;custom-menu&gt;<\/code>\u00a0\u51c6\u5907\u597d\u4e86\uff1a<\/p>\n<pre><code class=\"language-js\">customElements.define('custom-menu', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'});\n\n    \/\/ tmpl is the shadow DOM template (above)\n    this.shadowRoot.append( tmpl.content.cloneNode(true) );\n\n    \/\/ we can't select light DOM nodes, so let's handle clicks on the slot\n    this.shadowRoot.querySelector('slot[name=&quot;title&quot;]').onclick = () =&gt; {\n      \/\/ open\/close the menu\n      this.shadowRoot.querySelector('.menu').classList.toggle('closed');\n    };\n  }\n});\n<\/code><\/pre>\n<h2>\u66f4\u65b0\u63d2\u69fd<\/h2>\n<p>\u5982\u679c\u5916\u90e8\u4ee3\u7801\u60f3\u52a8\u6001 \u6dfb\u52a0\/\u79fb\u9664 \u83dc\u5355\u9879\u600e\u4e48\u529e\uff1f<br \/>\n<strong>\u5982\u679c \u6dfb\u52a0\/\u5220\u9664 \u4e86\u63d2\u69fd\u5143\u7d20\uff0c\u6d4f\u89c8\u5668\u5c06\u76d1\u89c6\u63d2\u69fd\u5e76\u66f4\u65b0\u6e32\u67d3\u3002<\/strong><br \/>\n\u53e6\u5916\uff0c\u7531\u4e8e\u4e0d\u590d\u5236 light DOM \u8282\u70b9\uff0c\u800c\u662f\u4ec5\u5728\u63d2\u69fd\u4e2d\u8fdb\u884c\u6e32\u67d3\uff0c\u6240\u4ee5\u5185\u90e8\u7684\u53d8\u5316\u662f\u7acb\u5373\u53ef\u89c1\u7684\u3002<br \/>\n\u56e0\u6b64\u6211\u4eec\u65e0\u9700\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u5373\u53ef\u66f4\u65b0\u6e32\u67d3\u3002\u4f46\u662f\u5982\u679c\u7ec4\u4ef6\u60f3\u77e5\u9053\u63d2\u69fd\u7684\u66f4\u6539\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528\u00a0<code>slotchange<\/code>\u00a0\u4e8b\u4ef6\u3002<br \/>\n\u4f8b\u5982\uff0c\u8fd9\u91cc\u7684\u83dc\u5355\u9879\u5728 1 \u79d2\u540e\u52a8\u6001\u63d2\u5165\uff0c\u800c\u4e14\u6807\u9898\u5728 2 \u79d2\u540e\u6539\u53d8\u3002<\/p>\n<pre><code class=\"language-html\">&lt;custom-menu id=&quot;menu&quot;&gt;\n  &lt;span slot=&quot;title&quot;&gt;Candy menu&lt;\/span&gt;\n&lt;\/custom-menu&gt;\n\n&lt;script&gt;\ncustomElements.define('custom-menu', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.innerHTML = `&lt;div class=&quot;menu&quot;&gt;\n      &lt;slot name=&quot;title&quot;&gt;&lt;\/slot&gt;\n      &lt;ul&gt;&lt;slot name=&quot;item&quot;&gt;&lt;\/slot&gt;&lt;\/ul&gt;\n    &lt;\/div&gt;`;\n\n    \/\/ shadowRoot can't have event handlers, so using the first child\n    this.shadowRoot.firstElementChild.addEventListener('slotchange',\n      e =&gt; alert(&quot;slotchange: &quot; + e.target.name)\n    );\n  }\n});\n\nsetTimeout(() =&gt; {\n  menu.insertAdjacentHTML('beforeEnd', '&lt;li slot=&quot;item&quot;&gt;Lollipop&lt;\/li&gt;')\n}, 1000);\n\nsetTimeout(() =&gt; {\n  menu.querySelector('[slot=&quot;title&quot;]').innerHTML = &quot;New menu&quot;;\n}, 2000);\n&lt;\/script&gt;\n<\/code><\/pre>\n<p>\u83dc\u5355\u6bcf\u6b21\u90fd\u4f1a\u66f4\u65b0\u6e32\u67d3\u800c\u65e0\u9700\u6211\u4eec\u5e72\u9884\u3002<br \/>\n\u8fd9\u91cc\u6709\u4e24\u4e2a\u00a0<code>slotchange<\/code>\u00a0\u4e8b\u4ef6\uff1a<\/p>\n<ol>\n<li>\u5728\u521d\u59cb\u5316\u65f6:<br \/>\n<code>slotchange: title<\/code>\u00a0\u7acb\u5373\u89e6\u53d1, \u56e0\u4e3a\u6765\u81ea light DOM \u7684\u00a0<code>slot=&quot;title&quot;<\/code>\u00a0\u8fdb\u5165\u4e86\u76f8\u5e94\u7684\u63d2\u69fd\u3002<\/li>\n<li>1 \u79d2\u540e:<br \/>\n<code>slotchange: item<\/code>\u00a0\u89e6\u53d1, \u5f53\u4e00\u4e2a\u65b0\u7684\u00a0<code>&lt;li slot=&quot;item&quot;&gt;<\/code>\u00a0\u88ab\u6dfb\u52a0\u3002<br \/>\n\u8bf7\u6ce8\u610f\uff1a2 \u79d2\u540e\uff0c\u5982\u679c\u4fee\u6539\u4e86\u00a0<code>slot=&quot;title&quot;<\/code>\u00a0\u7684\u5185\u5bb9\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u00a0<code>slotchange<\/code>\u00a0\u4e8b\u4ef6\u3002\u56e0\u4e3a\u6ca1\u6709\u63d2\u69fd\u66f4\u6539\u3002\u6211\u4eec\u4fee\u6539\u4e86 slotted \u5143\u7d20\u7684\u5185\u5bb9\uff0c\u8fd9\u662f\u53e6\u4e00\u56de\u4e8b\u3002<br \/>\n\u5982\u679c\u6211\u4eec\u60f3\u901a\u8fc7 JavaScript \u8ddf\u8e2a light DOM \u7684\u5185\u90e8\u4fee\u6539\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u66f4\u901a\u7528\u7684\u673a\u5236:\u00a0<a href=\"https:\/\/zh.javascript.info\/mutation-observer\">MutationObserver<\/a>\u3002<\/li>\n<\/ol>\n<h2>\u63d2\u69fd API<\/h2>\n<p>\u6700\u540e\u8ba9\u6211\u4eec\u6765\u8c08\u8c08\u4e0e\u63d2\u69fd\u76f8\u5173\u7684 JavaScript \u65b9\u6cd5\u3002<br \/>\n\u6b63\u5982\u6211\u4eec\u4e4b\u524d\u6240\u89c1\uff0cJavaScript \u4f1a\u67e5\u770b\u771f\u5b9e\u7684 DOM\uff0c\u4e0d\u5c55\u5f00\u3002\u4f46\u662f\u5982\u679c shadow \u6811\u6709\u00a0<code>{mode: 'open'}<\/code>\u00a0\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u627e\u51fa\u54ea\u4e2a\u5143\u7d20\u88ab\u653e\u8fdb\u4e00\u4e2a\u63d2\u69fd\uff0c\u53cd\u4e4b\u4ea6\u7136\uff0c\u54ea\u4e2a\u63d2\u69fd\u5206\u914d\u4e86\u7ed9\u8fd9\u4e2a\u5143\u7d20\uff1a<\/p>\n<ul>\n<li><code>node.assignedSlot<\/code>\u00a0\u2013 \u8fd4\u56de\u00a0<code>node<\/code>\u00a0\u5206\u914d\u7ed9\u7684\u00a0<code>&lt;slot&gt;<\/code>\u00a0\u5143\u7d20\u3002<\/li>\n<li><code>slot.assignedNodes({flatten: true\/false})<\/code>\u00a0\u2013 \u5206\u914d\u7ed9\u63d2\u69fd\u7684 DOM \u8282\u70b9\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>flatten<\/code>\u00a0\u9009\u9879\u4e3a\u00a0<code>false<\/code>\u3002\u5982\u679c\u663e\u5f0f\u5730\u8bbe\u7f6e\u4e3a\u00a0<code>true<\/code>\uff0c\u5219\u5b83\u5c06\u66f4\u6df1\u5165\u5730\u67e5\u770b\u6241\u5e73\u5316 DOM \uff0c\u5982\u679c\u5d4c\u5957\u4e86\u7ec4\u4ef6\uff0c\u5219\u8fd4\u56de\u5d4c\u5957\u7684\u63d2\u69fd\uff0c\u5982\u679c\u672a\u5206\u914d\u8282\u70b9\uff0c\u5219\u8fd4\u56de\u5907\u7528\u5185\u5bb9\u3002<\/li>\n<li><code>slot.assignedElements({flatten: true\/false})<\/code>\u00a0\u2013 \u5206\u914d\u7ed9\u63d2\u69fd\u7684 DOM \u5143\u7d20\uff08\u4e0e\u4e0a\u9762\u76f8\u540c\uff0c\u4f46\u4ec5\u5143\u7d20\u8282\u70b9\uff09\u3002<br \/>\n\u5f53\u6211\u4eec\u4e0d\u4ec5\u9700\u8981\u663e\u793a\u5df2\u63d2\u5165\u5185\u5bb9\u7684\u5185\u5bb9\uff0c\u8fd8\u9700\u8981\u5728 JavaScript \u4e2d\u5bf9\u5176\u8fdb\u884c\u8ddf\u8e2a\u65f6\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u975e\u5e38\u6709\u7528\u3002<br \/>\n\u4f8b\u5982\uff0c\u5982\u679c\u00a0<code>&lt;custom-menu&gt;<\/code>\u00a0\u7ec4\u4ef6\u60f3\u77e5\u9053\u5b83\u6240\u663e\u793a\u7684\u5185\u5bb9\uff0c\u90a3\u4e48\u5b83\u53ef\u4ee5\u8ddf\u8e2a\u00a0<code>slotchange<\/code>\u00a0\u5e76\u4ece\u00a0<code>slot.assignedElements<\/code>\u00a0\u83b7\u53d6\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-html\">&lt;custom-menu id=&quot;menu&quot;&gt;\n  &lt;span slot=&quot;title&quot;&gt;Candy menu&lt;\/span&gt;\n  &lt;li slot=&quot;item&quot;&gt;Lollipop&lt;\/li&gt;\n  &lt;li slot=&quot;item&quot;&gt;Fruit Toast&lt;\/li&gt;\n&lt;\/custom-menu&gt;\n\n&lt;script&gt;\ncustomElements.define('custom-menu', class extends HTMLElement {\n  items = []\n\n  connectedCallback() {\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.innerHTML = `&lt;div class=&quot;menu&quot;&gt;\n      &lt;slot name=&quot;title&quot;&gt;&lt;\/slot&gt;\n      &lt;ul&gt;&lt;slot name=&quot;item&quot;&gt;&lt;\/slot&gt;&lt;\/ul&gt;\n    &lt;\/div&gt;`;\n\n    \/\/ \u63d2\u69fd\u80fd\u88ab\u6dfb\u52a0\/\u5220\u9664\/\u4ee3\u66ff\n    this.shadowRoot.firstElementChild.addEventListener('slotchange', e =&gt; {\n      let slot = e.target;\n      if (slot.name == 'item') {\n        this.items = slot.assignedElements().map(elem =&gt; elem.textContent);\n        alert(&quot;Items: &quot; + this.items);\n      }\n    });\n  }\n});\n\n\/\/ items \u5728 1 \u79d2\u540e\u66f4\u65b0\nsetTimeout(() =&gt; {\n  menu.insertAdjacentHTML('beforeEnd', '&lt;li slot=&quot;item&quot;&gt;Cup Cake&lt;\/li&gt;')\n}, 1000);\n&lt;\/script&gt;\n<\/code><\/pre>\n<h1>\u7ed9 Shadow DOM \u6dfb\u52a0\u6837\u5f0f<\/h1>\n<p>shadow DOM \u53ef\u4ee5\u5305\u542b\u00a0<code>&lt;style&gt;<\/code>\u00a0\u548c\u00a0<code>&lt;link rel=&quot;stylesheet&quot; href=&quot;\u2026&quot;&gt;<\/code>\u00a0\u6807\u7b7e\u3002\u5728\u540e\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u6837\u5f0f\u8868\u662f HTTP \u7f13\u5b58\u7684\uff0c\u56e0\u6b64\u4e0d\u4f1a\u4e3a\u4f7f\u7528\u540c\u4e00\u6a21\u677f\u7684\u591a\u4e2a\u7ec4\u4ef6\u91cd\u65b0\u4e0b\u8f7d\u6837\u5f0f\u8868\u3002<br \/>\n\u4e00\u822c\u6765\u8bf4\uff0c\u5c40\u90e8\u6837\u5f0f\u53ea\u5728 shadow \u6811\u5185\u8d77\u4f5c\u7528\uff0c\u6587\u6863\u6837\u5f0f\u5728 shadow \u6811\u5916\u8d77\u4f5c\u7528\u3002\u4f46\u4e5f\u6709\u5c11\u6570\u4f8b\u5916\u3002<\/p>\n<h2>:host<\/h2>\n<p><code>:host<\/code>\u00a0\u9009\u62e9\u5668\u5141\u8bb8\u9009\u62e9 shadow \u5bbf\u4e3b\uff08\u5305\u542b shadow \u6811\u7684\u5143\u7d20\uff09\u3002<br \/>\n\u4f8b\u5982\uff0c\u6211\u4eec\u6b63\u5728\u521b\u5efa\u00a0<code>&lt;custom-dialog&gt;<\/code>\u00a0\u5143\u7d20\uff0c\u5e76\u4e14\u60f3\u4f7f\u5b83\u5c45\u4e2d\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u00a0<code>&lt;custom-dialog&gt;<\/code>\u00a0\u5143\u7d20\u672c\u8eab\u8bbe\u7f6e\u6837\u5f0f\u3002<br \/>\n\u8fd9\u6b63\u662f\u00a0<code>:host<\/code>\u00a0\u6240\u80fd\u505a\u7684\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;template id=&quot;tmpl&quot;&gt;\n  &lt;style&gt;\n    \/* \u8fd9\u4e9b\u6837\u5f0f\u5c06\u4ece\u5185\u90e8\u5e94\u7528\u5230 custom-dialog \u5143\u7d20\u4e0a *\/\n    :host {\n      position: fixed;\n      left: 50%;\n      top: 50%;\n      transform: translate(-50%, -50%);\n      display: inline-block;\n      border: 1px solid red;\n      padding: 10px;\n    }\n  &lt;\/style&gt;\n  &lt;slot&gt;&lt;\/slot&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\ncustomElements.define('custom-dialog', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'}).append(tmpl.content.cloneNode(true));\n  }\n});\n&lt;\/script&gt;\n\n&lt;custom-dialog&gt;\n  Hello!\n&lt;\/custom-dialog&gt;\n<\/code><\/pre>\n<h2>\u7ea7\u8054<\/h2>\n<p>shadow \u5bbf\u4e3b\uff08\u00a0<code>&lt;custom-dialog&gt;<\/code>\u00a0\u672c\u8eab\uff09\u9a7b\u7559\u5728 light DOM \u4e2d\uff0c\u56e0\u6b64\u5b83\u53d7\u5230\u6587\u6863 CSS \u89c4\u5219\u7684\u5f71\u54cd\u3002<br \/>\n\u5982\u679c\u5728\u5c40\u90e8\u7684\u00a0<code>:host<\/code>\u00a0\u548c\u6587\u6863\u4e2d\u90fd\u7ed9\u4e00\u4e2a\u5c5e\u6027\u8bbe\u7f6e\u6837\u5f0f\uff0c\u90a3\u4e48\u6587\u6863\u6837\u5f0f\u4f18\u5148\u3002<br \/>\n\u4f8b\u5982\uff0c\u5982\u679c\u5728\u6587\u6863\u4e2d\u6211\u4eec\u6709\u5982\u4e0b\u6837\u5f0f\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;style&gt;\ncustom-dialog {\n  padding: 0;\n}\n&lt;\/style&gt;\n<\/code><\/pre>\n<p>\u2026\u2026\u90a3\u4e48\u00a0<code>&lt;custom-dialog&gt;<\/code>\u00a0\u5c06\u6ca1\u6709 padding\u3002<br \/>\n\u8fd9\u662f\u975e\u5e38\u6709\u5229\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u53ef\u4ee5\u5728\u5176\u00a0<code>:host<\/code>\u00a0\u89c4\u5219\u4e2d\u8bbe\u7f6e \u201c\u9ed8\u8ba4\u201d \u7ec4\u4ef6\u6837\u5f0f\uff0c\u7136\u540e\u5728\u6587\u6863\u4e2d\u8f7b\u677e\u5730\u8986\u76d6\u5b83\u4eec\u3002<br \/>\n\u552f\u4e00\u7684\u4f8b\u5916\u662f\u5f53\u5c40\u90e8\u5c5e\u6027\u88ab\u6807\u8bb0\u00a0<code>!important<\/code>\u00a0\u65f6\uff0c\u5bf9\u4e8e\u8fd9\u6837\u7684\u5c5e\u6027\uff0c\u5c40\u90e8\u6837\u5f0f\u4f18\u5148\u3002<\/p>\n<h2>:host(selector)<\/h2>\n<p>\u4e0e\u00a0<code>:host<\/code>\u00a0\u76f8\u540c\uff0c\u4f46\u4ec5\u5728 shadow \u5bbf\u4e3b\u4e0e\u00a0<code>selector<\/code>\u00a0\u5339\u914d\u65f6\u624d\u5e94\u7528\u6837\u5f0f\u3002<br \/>\n\u4f8b\u5982\uff0c\u6211\u4eec\u5e0c\u671b\u4ec5\u5f53\u00a0<code>&lt;custom-dialog&gt;<\/code>\u00a0\u5177\u6709\u00a0<code>centered<\/code>\u00a0\u5c5e\u6027\u65f6\u624d\u5c06\u5176\u5c45\u4e2d:<\/p>\n<pre><code class=\"language-html\">&lt;template id=&quot;tmpl&quot;&gt;\n  &lt;style&gt;\n    :host([centered]) {\n      position: fixed;\n      left: 50%;\n      top: 50%;\n      transform: translate(-50%, -50%);\n      border-color: blue;\n    }\n\n    :host {\n      display: inline-block;\n      border: 1px solid red;\n      padding: 10px;\n    }\n  &lt;\/style&gt;\n  &lt;slot&gt;&lt;\/slot&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\ncustomElements.define('custom-dialog', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'}).append(tmpl.content.cloneNode(true));\n  }\n});\n&lt;\/script&gt;\n\n\n&lt;custom-dialog centered&gt;\n  Centered!\n&lt;\/custom-dialog&gt;\n\n&lt;custom-dialog&gt;\n  Not centered.\n&lt;\/custom-dialog&gt;\n<\/code><\/pre>\n<h2>:host-context(selector)<\/h2>\n<p>\u4e0e\u00a0<code>:host<\/code>\u00a0\u76f8\u540c\uff0c\u4f46\u4ec5\u5f53\u5916\u90e8\u6587\u6863\u4e2d\u7684 shadow \u5bbf\u4e3b\u6216\u5b83\u7684\u4efb\u4f55\u7956\u5148\u8282\u70b9\u4e0e\u00a0<code>selector<\/code>\u00a0\u5339\u914d\u65f6\u624d\u5e94\u7528\u6837\u5f0f\u3002<br \/>\n\u4f8b\u5982\uff0c<code>:host-context(.dark-theme)<\/code>\u00a0\u53ea\u6709\u5728\u00a0<code>&lt;custom-dialog&gt;<\/code>\u00a0\u6216\u8005\u00a0<code>&lt;custom-dialog&gt;<\/code>\u00a0\u7684\u4efb\u4f55\u7956\u5148\u8282\u70b9\u4e0a\u6709\u00a0<code>dark-theme<\/code>\u00a0\u7c7b\u65f6\u624d\u5339\u914d\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;body class=&quot;dark-theme&quot;&gt;\n  &lt;!--\n    :host-context(.dark-theme) \u53ea\u5e94\u7528\u4e8e .dark-theme \u5185\u90e8\u7684 custom-dialog\n  --&gt;\n  &lt;custom-dialog&gt;...&lt;\/custom-dialog&gt;\n&lt;\/body&gt;\n<\/code><\/pre>\n<p>\u603b\u4e4b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u00a0<code>:host<\/code>-family \u7cfb\u5217\u7684\u9009\u62e9\u5668\u6765\u5bf9\u7ec4\u4ef6\u7684\u4e3b\u5143\u7d20\u8fdb\u884c\u6837\u5f0f\u8bbe\u7f6e\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4e0a\u4e0b\u6587\u3002\u8fd9\u4e9b\u6837\u5f0f\uff08\u9664\u00a0<code>!important<\/code>\u00a0\u5916\uff09\u53ef\u4ee5\u88ab\u6587\u6863\u6837\u5f0f\u8986\u76d6\u3002<\/p>\n<h2>\u7ed9\u5360\u69fd\u5185\u5bb9\u6dfb\u52a0\u6837\u5f0f<\/h2>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u8003\u8651\u6709\u63d2\u69fd\u7684\u60c5\u51b5\u3002<br \/>\n\u5360\u69fd\u5143\u7d20\u6765\u81ea light DOM\uff0c\u6240\u4ee5\u5b83\u4eec\u4f7f\u7528\u6587\u6863\u6837\u5f0f\u3002\u5c40\u90e8\u6837\u5f0f\u4e0d\u4f1a\u5f71\u54cd\u5360\u69fd\u5185\u5bb9\u3002<br \/>\n\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6309\u7167\u6587\u6863\u6837\u5f0f\uff0c\u5360\u69fd\u7684\u00a0<code>&lt;span&gt;<\/code>\u00a0\u662f\u7c97\u4f53\uff0c\u4f46\u662f\u5b83\u4e0d\u4ece\u5c40\u90e8\u6837\u5f0f\u4e2d\u83b7\u53d6\u00a0<code>background<\/code>\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;style&gt;\n  span { font-weight: bold }\n&lt;\/style&gt;\n\n&lt;user-card&gt;\n  &lt;div slot=&quot;username&quot;&gt;&lt;span&gt;John Smith&lt;\/span&gt;&lt;\/div&gt;\n&lt;\/user-card&gt;\n\n&lt;script&gt;\ncustomElements.define('user-card', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.innerHTML = `\n      &lt;style&gt;\n      span { background: red; }\n      &lt;\/style&gt;\n      Name: &lt;slot name=&quot;username&quot;&gt;&lt;\/slot&gt;\n    `;\n  }\n});\n&lt;\/script&gt;\n<\/code><\/pre>\n<p>\u7ed3\u679c\u662f\u7c97\u4f53\uff0c\u4f46\u4e0d\u662f\u7ea2\u8272\u3002<br \/>\n\u5982\u679c\u6211\u4eec\u60f3\u8981\u5728\u6211\u4eec\u7684\u7ec4\u4ef6\u4e2d\u8bbe\u7f6e\u5360\u69fd\u5143\u7d20\u7684\u6837\u5f0f\uff0c\u6709\u4e24\u79cd\u9009\u62e9\u3002<br \/>\n\u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u00a0<code>&lt;slot&gt;<\/code>\u00a0\u672c\u8eab\u8fdb\u884c\u6837\u5f0f\u5316\uff0c\u5e76\u501f\u52a9 CSS \u7ee7\u627f\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;user-card&gt;\n  &lt;div slot=&quot;username&quot;&gt;&lt;span&gt;John Smith&lt;\/span&gt;&lt;\/div&gt;\n&lt;\/user-card&gt;\n\n&lt;script&gt;\ncustomElements.define('user-card', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.innerHTML = `\n      &lt;style&gt;\n      slot[name=&quot;username&quot;] { font-weight: bold; }\n      &lt;\/style&gt;\n      Name: &lt;slot name=&quot;username&quot;&gt;&lt;\/slot&gt;\n    `;\n  }\n});\n&lt;\/script&gt;\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u00a0<code>&lt;p&gt;John Smith&lt;\/p&gt;<\/code>\u00a0\u53d8\u6210\u7c97\u4f53\uff0c\u56e0\u4e3a CSS \u7ee7\u627f\u5728\u00a0<code>&lt;slot&gt;<\/code>\u00a0\u548c\u5b83\u7684\u5185\u5bb9\u4e4b\u95f4\u6709\u6548\u3002\u4f46\u662f\u5728 CSS \u4e2d\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u7684\u5c5e\u6027\u90fd\u662f\u7ee7\u627f\u7684\u3002<br \/>\n\u53e6\u4e00\u4e2a\u9009\u9879\u662f\u4f7f\u7528\u00a0<code>::slotted(selector)<\/code>\u00a0\u4f2a\u7c7b\u3002\u5b83\u6839\u636e\u4e24\u4e2a\u6761\u4ef6\u6765\u5339\u914d\u5143\u7d20\uff1a<\/p>\n<ol>\n<li>\u8fd9\u662f\u4e00\u4e2a\u5360\u69fd\u5143\u7d20\uff0c\u6765\u81ea\u4e8e light DOM\u3002\u63d2\u69fd\u540d\u5e76\u4e0d\u91cd\u8981\uff0c\u4efb\u4f55\u5360\u69fd\u5143\u7d20\u90fd\u53ef\u4ee5\uff0c\u4f46\u53ea\u80fd\u662f\u5143\u7d20\u672c\u8eab\uff0c\u800c\u4e0d\u662f\u5b83\u7684\u5b50\u5143\u7d20 \u3002<\/li>\n<li>\u8be5\u5143\u7d20\u4e0e\u00a0<code>selector<\/code>\u00a0\u5339\u914d\u3002<br \/>\n\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c<code>::slotted(div)<\/code>\u00a0\u6b63\u597d\u9009\u62e9\u4e86\u00a0<code>&lt;div slot=&quot;username&quot;&gt;<\/code>\u00a0\uff0c\u4f46\u662f\u6ca1\u6709\u9009\u62e9\u5b83\u7684\u5b50\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-html\">&lt;user-card&gt;\n  &lt;div slot=&quot;username&quot;&gt;\n    &lt;div&gt;John Smith&lt;\/div&gt;\n  &lt;\/div&gt;\n&lt;\/user-card&gt;\n\n&lt;script&gt;\ncustomElements.define('user-card', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.innerHTML = `\n      &lt;style&gt;\n      ::slotted(div) { border: 1px solid red; }\n      &lt;\/style&gt;\n      Name: &lt;slot name=&quot;username&quot;&gt;&lt;\/slot&gt;\n    `;\n  }\n});\n&lt;\/script&gt;\n<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c<code>::slotted<\/code>\u00a0\u9009\u62e9\u5668\u4e0d\u80fd\u7528\u4e8e\u4efb\u4f55\u63d2\u69fd\u4e2d\u66f4\u6df1\u5c42\u7684\u5185\u5bb9\u3002\u4e0b\u9762\u8fd9\u4e9b\u9009\u62e9\u5668\u662f\u65e0\u6548\u7684\uff1a<\/p>\n<pre><code class=\"language-css\">::slotted(div span) {\n  \/* \u6211\u4eec\u63d2\u5165\u7684 &lt;div&gt; \u4e0d\u4f1a\u5339\u914d\u8fd9\u4e2a\u9009\u62e9\u5668 *\/\n}\n\n::slotted(div) p {\n  \/* \u4e0d\u80fd\u8fdb\u5165 light DOM \u4e2d\u9009\u62e9\u5143\u7d20 *\/\n}\n<\/code><\/pre>\n<p>\u6b64\u5916\uff0c<code>::sloated<\/code>\u00a0\u53ea\u80fd\u5728 CSS \u4e2d\u4f7f\u7528\uff0c\u4e0d\u80fd\u5728\u00a0<code>querySelector<\/code>\u00a0\u4e2d\u4f7f\u7528\u3002<\/p>\n<h2>\u7528\u81ea\u5b9a\u4e49 CSS \u5c5e\u6027\u4f5c\u4e3a\u52fe\u5b50<\/h2>\n<p>\u5982\u4f55\u5728\u4e3b\u6587\u6863\u4e2d\u8bbe\u7f6e\u7ec4\u4ef6\u7684\u5185\u5efa\u5143\u7d20\u7684\u6837\u5f0f?<br \/>\n\u50cf\u00a0<code>:host<\/code>\u00a0\u8fd9\u6837\u7684\u9009\u62e9\u5668\u5e94\u7528\u89c4\u5219\u5230\u00a0<code>&lt;custom-dialog&gt;<\/code>\u00a0\u5143\u7d20\u6216\u00a0<code>&lt;user-card&gt;<\/code>\uff0c\u4f46\u662f\u5982\u4f55\u8bbe\u7f6e\u5728\u5b83\u4eec\u5185\u90e8\u7684 shadow DOM \u5143\u7d20\u7684\u6837\u5f0f\u5462\uff1f<br \/>\n\u6ca1\u6709\u9009\u62e9\u5668\u53ef\u4ee5\u4ece\u6587\u6863\u4e2d\u76f4\u63a5\u5f71\u54cd shadow DOM \u6837\u5f0f\u3002\u4f46\u662f\uff0c\u6b63\u5982\u6211\u4eec\u66b4\u9732\u7528\u6765\u4e0e\u7ec4\u4ef6\u4ea4\u4e92\u7684\u65b9\u6cd5\u90a3\u6837\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u66b4\u9732 CSS \u53d8\u91cf\uff08\u81ea\u5b9a\u4e49 CSS \u5c5e\u6027\uff09\u6765\u5bf9\u5176\u8fdb\u884c\u6837\u5f0f\u8bbe\u7f6e\u3002<br \/>\n<strong>\u81ea\u5b9a\u4e49 CSS \u5c5e\u6027\u5b58\u5728\u4e8e\u6240\u6709\u5c42\u6b21\uff0c\u5305\u62ec light DOM \u548c shadow DOM\u3002<\/strong><br \/>\n\u4f8b\u5982\uff0c\u5728 shadow DOM \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u00a0<code>--user-card-field-color<\/code>\u00a0CSS \u53d8\u91cf\u6765\u8bbe\u7f6e\u5b57\u6bb5\u7684\u6837\u5f0f\uff0c\u800c\u5916\u90e8\u6587\u6863\u53ef\u4ee5\u8bbe\u7f6e\u5b83\u7684\u503c\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;style&gt;\n  .field {\n    color: var(--user-card-field-color, black);\n    \/* \u5982\u679c --user-card-field-color \u6ca1\u6709\u88ab\u58f0\u660e\u8fc7\uff0c\u5219\u53d6\u503c\u4e3a black *\/\n  }\n&lt;\/style&gt;\n&lt;div class=&quot;field&quot;&gt;Name: &lt;slot name=&quot;username&quot;&gt;&lt;\/slot&gt;&lt;\/div&gt;\n&lt;div class=&quot;field&quot;&gt;Birthday: &lt;slot name=&quot;birthday&quot;&gt;&lt;\/slot&gt;&lt;\/div&gt;\n&lt;\/style&gt;\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5916\u90e8\u6587\u6863\u4e2d\u4e3a\u00a0<code>&lt;user-card&gt;<\/code>\u00a0\u58f0\u660e\u6b64\u5c5e\u6027\uff1a<\/p>\n<pre><code class=\"language-css\">user-card {\n  --user-card-field-color: green;\n}\n<\/code><\/pre>\n<p>\u81ea\u5b9a\u4e49 CSS \u5c5e\u6027\u7a7f\u900f shadow DOM\uff0c\u5b83\u4eec\u5728\u4efb\u4f55\u5730\u65b9\u90fd\u53ef\u89c1\uff0c\u56e0\u6b64\u5185\u90e8\u7684\u00a0<code>.field<\/code>\u00a0\u89c4\u5219\u5c06\u4f7f\u7528\u5b83\u3002<br \/>\n\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;style&gt;\n  user-card {\n    --user-card-field-color: green;\n  }\n&lt;\/style&gt;\n\n&lt;template id=&quot;tmpl&quot;&gt;\n  &lt;style&gt;\n    .field {\n      color: var(--user-card-field-color, black);\n    }\n  &lt;\/style&gt;\n  &lt;div class=&quot;field&quot;&gt;Name: &lt;slot name=&quot;username&quot;&gt;&lt;\/slot&gt;&lt;\/div&gt;\n  &lt;div class=&quot;field&quot;&gt;Birthday: &lt;slot name=&quot;birthday&quot;&gt;&lt;\/slot&gt;&lt;\/div&gt;\n&lt;\/template&gt;\n\n&lt;script&gt;\ncustomElements.define('user-card', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.append(document.getElementById('tmpl').content.cloneNode(true));\n  }\n});\n&lt;\/script&gt;\n\n&lt;user-card&gt;\n  &lt;span slot=&quot;username&quot;&gt;John Smith&lt;\/span&gt;\n  &lt;span slot=&quot;birthday&quot;&gt;01.01.2001&lt;\/span&gt;\n&lt;\/user-card&gt;\n<\/code><\/pre>\n<h2>Shadow DOM \u548c\u4e8b\u4ef6<\/h2>\n<p>Shadow tree \u80cc\u540e\u7684\u601d\u60f3\u662f\u5c01\u88c5\u7ec4\u4ef6\u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\u3002<br \/>\n\u5047\u8bbe\uff0c\u5728\u00a0<code>&lt;user-card&gt;<\/code>\u00a0\u7ec4\u4ef6\u7684 shadow DOM \u5185\u89e6\u53d1\u4e00\u4e2a\u70b9\u51fb\u4e8b\u4ef6\u3002\u4f46\u662f\u4e3b\u6587\u6863\u5185\u90e8\u7684\u811a\u672c\u5e76\u4e0d\u4e86\u89e3 shadow DOM \u5185\u90e8\uff0c\u5c24\u5176\u662f\u5f53\u7ec4\u4ef6\u6765\u81ea\u4e8e\u7b2c\u4e09\u65b9\u5e93\u3002<br \/>\n\u6240\u4ee5\uff0c\u4e3a\u4e86\u4fdd\u6301\u7ec6\u8282\u7b80\u5355\uff0c\u6d4f\u89c8\u5668\u4f1a<strong>\u91cd\u65b0\u5b9a\u4f4d<\/strong>\uff08retarget\uff09\u4e8b\u4ef6\u3002<br \/>\n<strong>\u5f53\u4e8b\u4ef6\u5728\u7ec4\u4ef6\u5916\u90e8\u6355\u83b7\u65f6\uff0cshadow DOM \u4e2d\u53d1\u751f\u7684\u4e8b\u4ef6\u5c06\u4f1a\u4ee5 host \u5143\u7d20\u4f5c\u4e3a\u76ee\u6807\u3002<\/strong><br \/>\n\u8fd9\u91cc\u6709\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;user-card&gt;&lt;\/user-card&gt;\n\n&lt;script&gt;\ncustomElements.define('user-card', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.innerHTML = `&lt;p&gt;\n      &lt;button&gt;Click me&lt;\/button&gt;\n    &lt;\/p&gt;`;\n    this.shadowRoot.firstElementChild.onclick =\n      e =&gt; alert(&quot;Inner target: &quot; + e.target.tagName);\n  }\n});\n\ndocument.onclick =\n  e =&gt; alert(&quot;Outer target: &quot; + e.target.tagName);\n&lt;\/script&gt;\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u70b9\u51fb\u4e86 button\uff0c\u5c31\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u4fe1\u606f\uff1a<\/p>\n<ol>\n<li>Inner target:\u00a0<code>BUTTON<\/code>\u00a0\u2014\u2014 \u5185\u90e8\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u83b7\u53d6\u4e86\u6b63\u786e\u7684\u76ee\u6807\uff0c\u5373 shadow DOM \u4e2d\u7684\u5143\u7d20\u3002<\/li>\n<li>Outer target:\u00a0<code>USER-CARD<\/code>\u00a0\u2014\u2014 \u6587\u6863\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4ee5 shadow host \u4f5c\u4e3a\u76ee\u6807\u3002<br \/>\n\u4e8b\u4ef6\u91cd\u5b9a\u5411\u662f\u4e00\u4ef6\u5f88\u68d2\u7684\u4e8b\u60c5\uff0c\u56e0\u4e3a\u5916\u90e8\u6587\u6863\u5e76\u4e0d\u9700\u8981\u77e5\u9053\u7ec4\u4ef6\u7684\u5185\u90e8\u60c5\u51b5\u3002\u4ece\u5b83\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u4e8b\u4ef6\u662f\u53d1\u751f\u5728\u00a0<code>&lt;user-card&gt;<\/code>\u3002<br \/>\n<strong>\u5982\u679c\u4e8b\u4ef6\u53d1\u751f\u5728 slotted \u5143\u7d20\u4e0a\uff0c\u5b9e\u9645\u5b58\u5728\u4e8e light DOM \u4e0a\uff0c\u5219\u4e0d\u4f1a\u53d1\u751f\u91cd\u5b9a\u5411\u3002<\/strong><br \/>\n\u4f8b\u5982\uff0c\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u7528\u6237\u70b9\u51fb\u4e86\u00a0<code>&lt;span slot=&quot;username&quot;&gt;<\/code>\uff0c\u90a3\u4e48\u5bf9\u4e8e shadow \u548c light \u5904\u7406\u7a0b\u5e8f\u6765\u8bf4\uff0c\u4e8b\u4ef6\u76ee\u6807\u5c31\u662f\u5f53\u524d\u8fd9\u4e2a\u00a0<code>span<\/code>\u00a0\u5143\u7d20\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-html\">&lt;user-card id=&quot;userCard&quot;&gt;\n  &lt;span slot=&quot;username&quot;&gt;John Smith&lt;\/span&gt;\n&lt;\/user-card&gt;\n\n&lt;script&gt;\ncustomElements.define('user-card', class extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({mode: 'open'});\n    this.shadowRoot.innerHTML = `&lt;div&gt;\n      &lt;b&gt;Name:&lt;\/b&gt; &lt;slot name=&quot;username&quot;&gt;&lt;\/slot&gt;\n    &lt;\/div&gt;`;\n\n    this.shadowRoot.firstElementChild.onclick =\n      e =&gt; alert(&quot;Inner target: &quot; + e.target.tagName);\n  }\n});\n\nuserCard.onclick = e =&gt; alert(`Outer target: ${e.target.tagName}`);\n&lt;\/script&gt;\n<\/code><\/pre>\n<p>\u5982\u679c\u5355\u51fb\u4e8b\u4ef6\u53d1\u751f\u5728\u00a0<code>&quot;John Smith&quot;<\/code>\u00a0\u4e0a\uff0c\u5219\u5bf9\u4e8e\u5185\u90e8\u548c\u5916\u90e8\u5904\u7406\u7a0b\u5e8f\u6765\u8bf4\uff0c\u5176\u76ee\u6807\u662f\u00a0<code>&lt;span slot=&quot;username&quot;&gt;<\/code>\u3002\u8fd9\u662f light DOM \u4e2d\u7684\u5143\u7d20\uff0c\u6240\u4ee5\u6ca1\u6709\u91cd\u5b9a\u5411\u3002<br \/>\n\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u5355\u51fb\u4e8b\u4ef6\u53d1\u751f\u5728\u6e90\u81ea shadow DOM \u7684\u5143\u7d20\u4e0a\uff0c\u4f8b\u5982\uff0c\u5728\u00a0<code>&lt;b&gt;Name&lt;\/b&gt;<\/code>\u00a0\u4e0a\uff0c\u7136\u540e\u5f53\u5b83\u5192\u6ce1\u51fa shadow DOM \u540e\uff0c\u5176\u00a0<code>event.target<\/code>\u00a0\u5c06\u91cd\u7f6e\u4e3a\u00a0<code>&lt;user-card&gt;<\/code>\u3002<\/p>\n<h2>\u5192\u6ce1 bubbling, event.composedPath()<\/h2>\n<p>\u51fa\u4e8e\u4e8b\u4ef6\u5192\u6ce1\u7684\u76ee\u7684\uff0c\u4f7f\u7528\u6241\u5e73 DOM\uff08flattened DOM\uff09\u3002<br \/>\n\u6240\u4ee5\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a slot \u5143\u7d20\uff0c\u5e76\u4e14\u4e8b\u4ef6\u53d1\u751f\u5728\u5b83\u7684\u5185\u90e8\u67d0\u4e2a\u5730\u65b9\uff0c\u90a3\u4e48\u5b83\u5c31\u4f1a\u5192\u6ce1\u5230\u00a0<code>&lt;slot&gt;<\/code>\u00a0\u5e76\u7ee7\u7eed\u5411\u4e0a\u3002<br \/>\n\u4f7f\u7528\u00a0<code>event.composedPath()<\/code>\u00a0\u83b7\u5f97\u539f\u59cb\u4e8b\u4ef6\u76ee\u6807\u7684\u5b8c\u6574\u8def\u5f84\u4ee5\u53ca\u6240\u6709 shadow \u5143\u7d20\u3002\u6b63\u5982\u6211\u4eec\u4ece\u65b9\u6cd5\u540d\u79f0\u4e2d\u770b\u5230\u7684\u90a3\u6837\uff0c\u8be5\u8def\u5f84\u662f\u5728\u7ec4\u5408\uff08composition\uff09\u4e4b\u540e\u83b7\u53d6\u7684\u3002<br \/>\n\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6241\u5e73 DOM \u662f\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;user-card id=&quot;userCard&quot;&gt;\n  #shadow-root\n    &lt;div&gt;\n      &lt;b&gt;Name:&lt;\/b&gt;\n      &lt;slot name=&quot;username&quot;&gt;\n        &lt;span slot=&quot;username&quot;&gt;John Smith&lt;\/span&gt;\n      &lt;\/slot&gt;\n    &lt;\/div&gt;\n&lt;\/user-card&gt;\n<\/code><\/pre>\n<p>\u56e0\u6b64\uff0c\u5bf9\u4e8e\u00a0<code>&lt;span slot=&quot;username&quot;&gt;<\/code>\u00a0\u4e0a\u7684\u70b9\u51fb\u4e8b\u4ef6\uff0c\u4f1a\u8c03\u7528\u00a0<code>event.composedPath()<\/code>\u00a0\u5e76\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff1a[<code>span<\/code>,\u00a0<code>slot<\/code>,\u00a0<code>div<\/code>,\u00a0<code>shadow-root<\/code>,\u00a0<code>user-card<\/code>,\u00a0<code>body<\/code>,\u00a0<code>html<\/code>,\u00a0<code>document<\/code>,\u00a0<code>window<\/code>]\u3002\u5728\u7ec4\u5408\u4e4b\u540e\uff0c\u8fd9\u6b63\u662f\u6241\u5e73 DOM \u4e2d\u76ee\u6807\u5143\u7d20\u7684\u7236\u94fe\u3002<\/p>\n<blockquote>\n<p>Shadow \u6811\u8be6\u7ec6\u4fe1\u606f\u4ec5\u63d0\u4f9b\u7ed9\u00a0<code>{mode:'open'}<\/code>\u00a0\u6811<br \/>\n\u5982\u679c shadow \u6811\u662f\u7528\u00a0<code>{mode: 'closed'}<\/code>\u00a0\u521b\u5efa\u7684\uff0c\u90a3\u4e48\u7ec4\u5408\u8def\u5f84\u5c31\u4ece host \u5f00\u59cb\uff1a<code>user-card<\/code>\u00a0\u53ca\u5176\u66f4\u4e0a\u5c42\u3002<br \/>\n\u8fd9\u4e0e\u4f7f\u7528 shadow DOM \u7684\u5176\u4ed6\u65b9\u6cd5\u7684\u539f\u7406\u7c7b\u4f3c\u3002closed \u6811\u5185\u90e8\u662f\u5b8c\u5168\u9690\u85cf\u7684\u3002<\/p>\n<\/blockquote>\n<h2>event.composed<\/h2>\n<p>\u5927\u591a\u6570\u4e8b\u4ef6\u80fd\u6210\u529f\u5192\u6ce1\u5230 shadow DOM \u8fb9\u754c\u3002\u5f88\u5c11\u6709\u4e8b\u4ef6\u4e0d\u80fd\u5192\u6ce1\u5230 shadow DOM \u8fb9\u754c\u3002<br \/>\n\u8fd9\u7531\u00a0<code>composed<\/code>\u00a0\u4e8b\u4ef6\u5bf9\u8c61\u5c5e\u6027\u63a7\u5236\u3002\u5982\u679c\u00a0<code>composed<\/code>\u00a0\u662f\u00a0<code>true<\/code>\uff0c\u90a3\u4e48\u4e8b\u4ef6\u5c31\u80fd\u7a7f\u8fc7\u8fb9\u754c\u3002\u5426\u5219\u5b83\u4ec5\u80fd\u5728 shadow DOM \u5185\u90e8\u6355\u83b7\u3002<br \/>\n\u5982\u679c\u4f60\u6d4f\u89c8\u4e00\u4e0b\u00a0<a href=\"https:\/\/www.w3.org\/TR\/uievents\">UI \u4e8b\u4ef6\u89c4\u8303<\/a>\u00a0\u5c31\u77e5\u9053\uff0c\u5927\u90e8\u5206\u4e8b\u4ef6\u90fd\u662f\u00a0<code>composed: true<\/code>\uff1a<\/p>\n<ul>\n<li><code>blur<\/code>\uff0c<code>focus<\/code>\uff0c<code>focusin<\/code>\uff0c<code>focusout<\/code>\uff0c<\/li>\n<li><code>click<\/code>\uff0c<code>dblclick<\/code>\uff0c<\/li>\n<li><code>mousedown<\/code>\uff0c<code>mouseup<\/code>\u00a0<code>mousemove<\/code>\uff0c<code>mouseout<\/code>\uff0c<code>mouseover<\/code>\uff0c<\/li>\n<li><code>wheel<\/code>\uff0c<\/li>\n<li><code>beforeinput<\/code>\uff0c<code>input<\/code>\uff0c<code>keydown<\/code>\uff0c<code>keyup<\/code>\u3002<br \/>\n\u6240\u6709\u89e6\u6478\u4e8b\u4ef6\uff08touch events\uff09\u53ca\u6307\u9488\u4e8b\u4ef6\uff08pointer events\uff09\u90fd\u662f\u00a0<code>composed: true<\/code>\u3002<br \/>\n\u4f46\u4e5f\u6709\u4e9b\u4e8b\u4ef6\u662f\u00a0<code>composed: false<\/code>\u00a0\u7684\uff1a<\/li>\n<li><code>mouseenter<\/code>\uff0c<code>mouseleave<\/code>\uff08\u5b83\u4eec\u6839\u672c\u4e0d\u4f1a\u5192\u6ce1\uff09\uff0c<\/li>\n<li><code>load<\/code>\uff0c<code>unload<\/code>\uff0c<code>abort<\/code>\uff0c<code>error<\/code>\uff0c<\/li>\n<li><code>select<\/code>\uff0c<\/li>\n<li><code>slotchange<\/code>\u3002<br \/>\n\u8fd9\u4e9b\u4e8b\u4ef6\u4ec5\u80fd\u5728\u4e8b\u4ef6\u76ee\u6807\u6240\u5728\u7684\u540c\u4e00 DOM \u4e2d\u7684\u5143\u7d20\u4e0a\u6355\u83b7\uff0c<\/li>\n<\/ul>\n<h2>\u81ea\u5b9a\u4e49\u4e8b\u4ef6\uff08Custom events\uff09<\/h2>\n<p>\u5f53\u6211\u4eec\u53d1\u9001\uff08dispatch\uff09\u81ea\u5b9a\u4e49\u4e8b\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u00a0<code>bubbles<\/code>\u00a0\u548c\u00a0<code>composed<\/code>\u00a0\u5c5e\u6027\u90fd\u4e3a\u00a0<code>true<\/code>\u00a0\u4ee5\u4f7f\u5176\u5192\u6ce1\u5e76\u4ece\u7ec4\u4ef6\u4e2d\u5192\u6ce1\u51fa\u6765\u3002<br \/>\n\u4f8b\u5982\uff0c\u6211\u4eec\u5728\u00a0<code>div#outer<\/code>\u00a0shadow DOM \u5185\u90e8\u521b\u5efa\u00a0<code>div#inner<\/code>\u00a0\u5e76\u5728\u5176\u4e0a\u89e6\u53d1\u4e24\u4e2a\u4e8b\u4ef6\u3002\u53ea\u6709\u00a0<code>composed: true<\/code>\u00a0\u7684\u90a3\u4e2a\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u624d\u4f1a\u8ba9\u8be5\u4e8b\u4ef6\u672c\u8eab\u5192\u6ce1\u5230\u6587\u6863\u5916\u9762\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;div id=&quot;outer&quot;&gt;&lt;\/div&gt;\n\n&lt;script&gt;\nouter.attachShadow({mode: 'open'});\n\nlet inner = document.createElement('div');\nouter.shadowRoot.append(inner);\n\n\/*\ndiv(id=outer)\n  #shadow-dom\n    div(id=inner)\n*\/\n\ndocument.addEventListener('test', event =&gt; alert(event.detail));\n\ninner.dispatchEvent(new CustomEvent('test', {\n  bubbles: true,\n  composed: true,\n  detail: &quot;composed&quot;\n}));\n\ninner.dispatchEvent(new CustomEvent('test', {\n  bubbles: true,\n  composed: false,\n  detail: &quot;not composed&quot;\n}));\n&lt;\/script&gt;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Custom elements \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u63cf\u8ff0\u5e26\u6709\u81ea\u5df1\u7684\u65b9\u6cd5\u3001\u5c5e\u6027\u548c\u4e8b\u4ef6\u7b49\u7684\u7c7b\u6765\u521b\u5efa\u81ea\u5b9a\u4e49 HTML \u5143\u7d20\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-752","post","type-post","status-publish","format-standard","hentry","category-front-end"],"_links":{"self":[{"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/posts\/752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/levin.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=752"}],"version-history":[{"count":1,"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/posts\/752\/revisions"}],"predecessor-version":[{"id":753,"href":"https:\/\/levin.ink\/index.php?rest_route=\/wp\/v2\/posts\/752\/revisions\/753"}],"wp:attachment":[{"href":"https:\/\/levin.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/levin.ink\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/levin.ink\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}