{"id":10125,"date":"2021-09-24T00:04:44","date_gmt":"2021-09-23T16:04:44","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10125"},"modified":"2022-01-06T15:02:20","modified_gmt":"2022-01-06T07:02:20","slug":"html-slot-dom","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/09\/html-slot-dom\/","title":{"rendered":"HTML slot \u63d2\u69fd\u5143\u7d20\u6df1\u5165"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=10125\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10125<\/a> \u946b\u7a7a\u95f4-\u946b\u751f\u6d3b<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\/202109-slot.png';if(!\/zhangxinxu\/.test(location.host)){document.body.addEventListener('mousedown',function(){location.href='https:\/\/www.zhangxinxu.com\/wordpress\/?p=10125'});}<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202109\/slot-cover.png\" width=\"484\" height=\"297\" alt=\"slot\u5360\u4f4d\u56fe\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u5f15\u8a00<\/h3>\n<p>\u65e0\u8bba\u662f\u5b9e\u4f8b\u7ec4\u4ef6\u8fd8\u662f HTML \u7ec4\u4ef6\uff0c\u4f20\u53c2\u90fd\u662f\u514d\u4e0d\u4e86\u7684\u3002<\/p>\n<p>\u4ee5 Web Components \u7ec4\u4ef6\u4e3e\u4f8b\uff0c\u6211\u4eec\u8981\u4f20\u9012\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 <code>width<\/code> \u6216 <code>height<\/code> \u5c5e\u6027\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;by-zxx width=\"300\" height=\"150\"&gt;&lt;\/by-zxx&gt;<\/pre>\n<p>\u4f46\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u4f20\u9012\u7684\u53c2\u6570\u662f\u4e00\u6bb5 HTML \u5185\u5bb9\uff0c\u8fd9\u4e2a\u65f6\u5019\uff0c\u8fd9\u6bb5 HTML \u8be5\u5982\u4f55\u4f20\u5165\u7ec4\u4ef6\u5185\uff1f<\/p>\n<p>\u6b64\u65f6\u5c31\u9700\u8981\u4f7f\u7528\u63d2\u69fd\u5143\u7d20 <code>&lt;slot&gt;<\/code>\u3002<\/p>\n<h3>\u4e8c\u3001Shadow \u4e2d\u624d\u6709\u7528<\/h3>\n<p><code>&lt;slot&gt;<\/code> \u5143\u7d20\u53ea\u80fd\u5728 Shadow DOM \u4e2d\u4f7f\u7528\u624d\u6709\u63d2\u69fd\u6548\u679c\uff0c\u5426\u5219\uff0c\u5c31\u53ef\u4ee5\u770b\u6210\u662f\u4e2a\u666e\u901a\u7684 HTML \u5143\u7d20\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u76f4\u63a5\u5728 <code>&lt;body&gt;<\/code> \u5143\u7d20\u4e0b\u7684\u8fd9\u6bb5 HTML \u4ee3\u7801\u662f\u65e0\u6548\u7684\uff0c\u5373\u56fe\u7247\u5143\u7d20\u662f\u65e0\u6cd5\u66ff\u6362\u201c\u5360\u4f4d\u5143\u7d20\u201d\u8fd9\u4e2a\u5185\u5bb9\u7684\uff1a<\/p>\n<pre>&lt;img src=\"1.png\" slot=\"some\"&gt;\r\n&lt;p&gt;\r\n    \u5185\u5bb9\uff1a&lt;slot name=\"some\"&gt;\u5360\u4f4d\u5143\u7d20&lt;\/slot&gt;\r\n&lt;\/p&gt;<\/pre>\n<p>\u53ef\u4ee5\u8bf4\uff0c<code>&lt;slot&gt;<\/code> \u5143\u7d20\u5c31\u662f\u4e3a Web Components \u7ec4\u4ef6\u5f00\u53d1\u800c\u8bbe\u8ba1\u7684\u3002<\/p>\n<h3>\u4e09\u3001\u57fa\u672c\u4f7f\u7528\u793a\u610f<\/h3>\n<p>\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5e26\u5927\u5bb6\u4e86\u89e3\u4e0b <code>&lt;slot&gt;<\/code> \u5143\u7d20\u7684\u4f5c\u7528\u3002<\/p>\n<p>\u4ee5 <code>alert<\/code> \u5f39\u6846\u7ec4\u4ef6\u793a\u610f\uff0c\u5f39\u6846\u7684\u7ed3\u6784\u5176\u5b9e\u662f\u56fa\u5b9a\u7684\uff0c\u53d8\u5316\u7684\u662f\u63d0\u793a\u7684\u5185\u5bb9\uff0c\u6b64\u65f6\u63d0\u793a\u5185\u5bb9\u5c31\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u4f20\u8fdb\u53bb\uff0c\u7136\u800c\uff0c\u6709\u65f6\u5019\u63d0\u793a\u5185\u5bb9\u7684\u7ed3\u6784\u4f1a\u6bd4\u8f83\u590d\u6742\uff0c\u662f\u4e00\u4e2a\u590d\u5408\u7684 DOM \u7ed3\u6784\uff0c\u5c31\u975e\u5e38\u9002\u5408\u4f7f\u7528 <code>&lt;slot&gt;<\/code> \u5143\u7d20\u5b9e\u73b0\u3002<\/p>\n<p>\u6211\u4eec\u4e0d\u59a8\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a &#8216;zxx-alert&#8217; \u7684\u5f39\u6846\u7ec4\u4ef6\uff0c\u4e3a\u4e86\u793a\u610f\u7b80\u660e\uff0c\u7ec4\u4ef6\u6837\u5f0f\u548c HTML \u7ed3\u6784\u6211\u4eec\u653e\u5728 <code>&lt;template&gt;<\/code> \u5143\u7d20\u4e2d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>&lt;template id=\"alertTpl\"&gt;\r\n    &lt;style&gt;\r\n        :host(:not([open])) {\r\n            display: none;\r\n        }\r\n        :host {\r\n            position: fixed;\r\n            left: 0; top: 0;\r\n            height: 100%; width: 100%;\r\n            background-color: rgba(25, 28, 34, 0.88);\r\n            z-index: 19;\r\n            display: grid;\r\n            place-items: center;\r\n        }\r\n        dialog {\r\n            position: static;\r\n            display: inherit;\r\n        }\r\n    &lt;\/style&gt;\r\n    &lt;dialog&gt;\r\n        &lt;slot name=\"alert\"&gt;\u6682\u65e0\u63d0\u793a\u4fe1\u606f&lt;\/slot&gt;\r\n        &lt;p&gt;\r\n            &lt;button&gt;\u786e\u5b9a&lt;\/button&gt;\r\n        &lt;\/p&gt;\r\n    &lt;\/dialog&gt;\r\n&lt;\/template&gt;<\/pre>\n<p>\u5176\u4e2d\uff0c\u5c31\u51fa\u73b0\u4e86\u63d2\u69fd\u5143\u7d20\uff0c\u5176 HTML \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>&lt;slot name=\"alert\"&gt;\u6682\u65e0\u63d0\u793a\u4fe1\u606f&lt;\/slot&gt;<\/pre>\n<p>\u8868\u793a\u8fd9\u90e8\u5206\u5185\u5bb9\u662f\u53ef\u4ee5\u5728\u5916\u90e8\uff0c\u88ab\u81ea\u5b9a\u4e49\u5143\u7d20\u4e2d\u7684\u5176\u4ed6 HTML \u66ff\u6362\u6389\u7684\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u8981\u60f3\u751f\u6548\uff0c\u9700\u8981\u8f6c\u6362\u6210 Shadow DOM \u5143\u7d20\uff0c\u5e76\u4f5c\u4e3a\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u7684\u4e00\u90e8\u5206\uff0c\u76f8\u5173\u6267\u884c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>customElements.define('zxx-alert', class extends HTMLElement {\r\n    constructor() {\r\n        super();\r\n        let contentDoc = document.getElementById('alertTpl').content;\r\n\r\n        const shadowRoot = this.attachShadow({\r\n            mode: 'open'\r\n        }).append(contentDoc.cloneNode(true));  \r\n    }\r\n});<\/pre>\n<p>\u6b64\u65f6\uff0c\u53ea\u9700\u8981\u5728 <code>&lt;zxx-alert&gt;<\/code> \u5143\u7d20\u5185\u8bbe\u7f6e\u597d\u5bf9\u5e94\u7684\u5339\u914d\u63d2\u69fd\u7684\u5185\u5bb9\uff08\u901a\u8fc7 <code>slot<\/code> \u5c5e\u6027\u548c <code>&lt;slot&gt;<\/code> \u5143\u7d20\u7684 <code>name<\/code> \u503c\u5339\u914d\uff09\uff0c\u8fd9\u90e8\u5206\u5185\u5bb9\u5c31\u53ef\u4ee5\u4f5c\u4e3a\u63d0\u793a\u4fe1\u606f\u51fa\u73b0\u4e86\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;zxx-alert open&gt;\r\n    &lt;p slot=\"alert\"&gt;\u63d2\u69fd\u6267\u884c\u6210\u529f\uff01&lt;\/p&gt;\r\n&lt;\/zxx-alert&gt;<\/pre>\n<p>\u6b64\u65f6\uff0c\u5c31\u6709\u7c7b\u4f3c\u4e0b\u56fe\u7684\u6548\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202109\/2021-09-23_200148.png\" width=\"184\" height=\"180\" alt=\"alert\u5f39\u6846\u63d2\u69fd\u5b9e\u73b0\u6548\u679c\"><\/p>\n<p>DOM \u5185\u5bb9\u4f5c\u4e3a\u53c2\u6570\u53d8\u6210\u4e86 alert \u63d0\u793a\u6846\u7684\u63d0\u793a\u5185\u5bb9\u4e86\uff0c\u53ef\u4ee5\u770b\u51fa\uff0c\u4f7f\u7528 <code>&lt;slot&gt;<\/code> \u5143\u7d20\u5b9e\u73b0\u52a8\u6001\u5185\u5bb9\u5448\u73b0\u4f1a\u975e\u5e38\u7684\u7075\u6d3b\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=10125\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<h3>\u56db\u3001\u63d2\u69fd\u5339\u914d\u89c4\u5219<\/h3>\n<p>\u4e00\u53e5\u8bdd\u6982\u62ec\u5c31\u662f\uff1a<\/p>\n<p><code>&lt;slot&gt;<\/code>  \u63d2\u69fd\u5143\u7d20\u7684<code>name<\/code> \u5c5e\u6027\u503c\u548c\u4efb\u610f HTML \u5143\u7d20\u7684 <code>slot<\/code> \u5c5e\u6027\u503c\u4e00\u81f4\u7684\u65f6\u5019\u4f1a\u88ab\u5339\u914d\u3002<\/p>\n<p>\u5177\u4f53\u7ec6\u8282\u5982\u4e0b\uff1a<\/p>\n<h4>1. name \u503c\u552f\u4e00<\/h4>\n<p><code>&lt;slot&gt;<\/code> \u652f\u6301 <code>name<\/code> \u5c5e\u6027\uff0c\u53ef\u4ee5\u770b\u6210\u662f\u8eab\u4efd\u6807\u8bc6\uff0c\u9700\u8981\u662f\u552f\u4e00\u7684\uff0c\u56e0\u4e3a\u591a\u4e2a\u76f8\u540c name \u6700\u591a\u53ea\u4f1a\u5339\u914d\u4e00\u4e2a\u63d2\u69fd\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>&lt;dialog&gt;\r\n    &lt;slot name=\"alert\"&gt;\u6682\u65e0\u63d0\u793a\u4fe1\u606f&lt;\/slot&gt;\r\n    &lt;p&gt;&lt;slot name=\"alert\"&gt;\u6682\u65e0\u63d0\u793a\u4fe1\u606f +1&lt;\/slot&gt;&lt;\/p&gt;\r\n    &lt;p&gt;\r\n        &lt;button&gt;\u786e\u5b9a&lt;\/button&gt;\r\n    &lt;\/p&gt;\r\n&lt;\/dialog&gt;<\/pre>\n<p>\u4e0b\u9762\u8fd9\u4e2a <code>name=\"alert\"<\/code> \u63d2\u69fd\u5143\u7d20\u5c31\u4e0d\u4f1a\u88ab HTML \u66ff\u6362\uff0c\u5982\u4e0b\u4e0b\u56fe\u6240\u793a\uff08\u4e0b\u9762\u8fd9\u884c\u6587\u5b57\uff09\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202201\/2022-01-06_150148.png\" width=\"206\" height=\"259\" alt=\"name\u591a\u4e2a\u7684\u65f6\u5019\u95ee\u9898\"><\/p>\n<h4>2. slot \u5c5e\u6027\u503c\u53ef\u4ee5\u4e0d\u552f\u4e00<\/h4>\n<p>\u800c <code>slot<\/code> \u5c5e\u6027\u503c\u53ef\u4ee5\u4e0d\u552f\u4e00\uff0c\u4f8b\u5982\u4e0b\u9762\u8fd9\u6bb5 HTML \u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>&lt;zxx-alert open&gt;\r\n    &lt;p slot=\"alert\"&gt;\u63d2\u69fd\u6267\u884c\u6210\u529f\uff01&lt;\/p&gt;\r\n    &lt;p slot=\"alert\"&gt;\u63d2\u69fd\u6267\u884c\u6210\u529f +1\uff01&lt;\/p&gt;\r\n&lt;\/zxx-alert&gt;<\/pre>\n<p>\u4f1a\u770b\u5230\u4e24\u6bb5\u6267\u884c\u6210\u529f\u7684\u63d0\u793a\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff08\u622a\u81ea Firefox \u6d4f\u89c8\u5668\uff09\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202109\/2021-09-23_181956.png\" width=\"228\" height=\"235\" alt=\"slot \u5c5e\u6027\u503c\u76f8\u540cFirefox\u4e0b\"><\/p>\n<h4>3. \u4e00\u4e2a\u7ec4\u4ef6\u53ef\u4ee5\u591a\u4e2a\u63d2\u69fd<\/h4>\n<p>\u540c\u4e00\u4e2a\u7ec4\u4ef6\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u63d2\u69fd\uff0c\u4f8b\u5982\uff0c\u9884\u7559\u4e00\u4e2a\u6309\u94ae\u7684\u4f4d\u7f6e\uff1a<\/p>\n<pre>&lt;dialog&gt;\r\n    &lt;slot name=\"alert\"&gt;\u6682\u65e0\u63d0\u793a\u4fe1\u606f&lt;\/slot&gt;\r\n    &lt;p&gt;\r\n        &lt;button&gt;\u786e\u5b9a&lt;\/button&gt;\r\n        &lt;slot name=\"button\"&gt;&lt;\/slot&gt;\r\n    &lt;\/p&gt;\r\n&lt;\/dialog&gt;<\/pre>\n<pre>&lt;zxx-alert open&gt;\r\n    &lt;p slot=\"alert\"&gt;\u63d2\u69fd\u6267\u884c\u6210\u529f\uff01&lt;\/p&gt;\r\n    &lt;button slot=\"button\"&gt;\u53d6\u6d88&lt;\/button&gt;\r\n&lt;\/zxx-alert&gt;<\/pre>\n<p>\u6b64\u65f6\u53ef\u4ee5\u770b\u5230\uff0c\u4e0d\u4ec5\u63d0\u793a\u5185\u5bb9\u88ab\u63d2\u5165\u4e86\uff0c\u53d6\u6d88\u6309\u94ae\u4e5f\u4e00\u5e76\u63d2\u5165\u5230\u4e86\u5f39\u6846\u4e4b\u4e2d\uff0c\u6548\u679c\u5982\u4e0b\u622a\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202109\/2021-09-23_195434.png\" width=\"172\" height=\"179\" alt=\"\u591a\u4e2a\u63d2\u69fd\u540c\u65f6\u4f7f\u7528\u793a\u610f\"><\/p>\n<h4>4. \u5fc5\u987b\u662f\u7ec4\u4ef6\u7684\u5b50\u5143\u7d20<\/h4>\n<p>\u7528\u6765\u5339\u914d\u7684\u5143\u7d20\u4e5f\u5fc5\u987b\u5199\u5728\u81ea\u5b9a\u4e49\u5143\u7d20\u7ec4\u4ef6\u4e2d\uff0c\u4f5c\u4e3a\u5b50\u5143\u7d20\u5b58\u5728\uff0c\u4f8b\u5982\u4e0b\u9762\u914d\u5bf9\u5143\u7d20\u5728\u81ea\u5b9a\u4e49\u5143\u7d20\u4e4b\u5916\uff0c\u662f\u6ca1\u6709\u63d2\u69fd\u6548\u679c\u7684\uff1a<\/p>\n<pre>&lt;p slot=\"alert\"&gt;\u63d2\u69fd\u6267\u884c\u6210\u529f\uff01&lt;\/p&gt;\r\n&lt;zxx-alert open&gt;&lt;\/zxx-alert&gt;<\/pre>\n<p>\u4ee5\u53ca\u4e0b\u9762\u8fd9\u79cd\uff0c\u5339\u914d\u5143\u7d20\u4e0d\u662f\u5b50\u5143\u7d20\uff0c\u800c\u662f\u5b50\u5143\u7d20\u7684\u5b50\u5143\u7d20\uff0c\u4e5f\u662f\u65e0\u6cd5\u4f5c\u4e3a\u6709\u6548\u7684\u63d2\u69fd\u663e\u793a\u7684\uff1a<\/p>\n<pre>&lt;zxx-alert open&gt;\r\n    &lt;p slot=\"alert\"&gt;\u63d2\u69fd\u6267\u884c\u6210\u529f\uff01&lt;\/p&gt;\r\n    &lt;div&gt;\r\n        &lt;button slot=\"button\"&gt;\u53d6\u6d88&lt;\/button&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/zxx-alert&gt;<\/pre>\n<p>\u6548\u679c\u5982\u4e0b\uff0c\u53ef\u4ee5\u770b\u5230\u5e76\u6ca1\u6709\u53d6\u6d88\u6309\u94ae\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202109\/2021-09-23_200148.png\" width=\"184\" height=\"180\" alt=\"\u53d6\u6d88\u6309\u94ae\u63d2\u69fd\u6ca1\u751f\u6548\"><\/p>\n<p>\u5373\u4f7f <code>&lt;div&gt;<\/code> \u5143\u7d20\u8bbe\u7f6e <code>display:contents<\/code> \u4e5f\u65e0\u6548\u3002<\/p>\n<h3>\u4e94\u3001slot \u5143\u7d20\u4e2d\u7684\u4e8b\u4ef6<\/h3>\n<p>\u4ee5\u5f39\u6846\u4e2d\u7684\u53d6\u6d88\u6309\u94ae\u4e3e\u4f8b\uff0c\u5982\u679c\u7ed9\u53d6\u6d88\u6309\u94ae\u7684\u63d2\u69fd\u6dfb\u52a0\u4e8b\u4ef6\u5462\uff1f<\/p>\n<pre>&lt;zxx-alert open&gt;\r\n    &lt;p slot=\"alert\"&gt;\u63d2\u69fd\u6267\u884c\u6210\u529f\uff01&lt;\/p&gt;\r\n    &lt;button slot=\"button\"&gt;\u53d6\u6d88&lt;\/button&gt;\r\n&lt;\/zxx-alert&gt;<\/pre>\n<p>\u662f\u8fd9\u6837\u7684\uff0c\u867d\u7136\u89c6\u89c9\u4e0a\uff0c\u5339\u914d\u5143\u7d20\u66ff\u6362\u4e86\u63d2\u69fd\u5143\u7d20\uff0c\u5b9e\u9645\u4e0a\uff0c\u4e24\u8005\u7684\u4f4d\u7f6e\u5e76\u672a\u53d1\u751f\u53d8\u5316\uff0c\u5176 HTML \u7ed3\u6784\u5173\u7cfb\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202109\/2021-09-23_200720.png\" width=\"335\" height=\"270\" alt=\"slot\u5143\u7d20\u4f4d\u7f6e\u5173\u7cfb\"><\/p>\n<p>\u56e0\u6b64\uff0c\u8981\u5b9e\u73b0\u70b9\u51fb\u5f39\u6846\u4e2d\u7684\u53d6\u6d88\u6309\u94ae\u5173\u95ed\u5f39\u6846\uff0c\u53ea\u9700\u8981\u5728\u539f\u59cb\u7684 <code>&lt;slot&gt;<\/code> \u5143\u7d20\u6216\u8005\u5339\u914d\u7684 <code>&lt;button&gt;<\/code> \u5143\u7d20\u4e0a\u5199\u4e8b\u4ef6\u5c31\u53ef\u4ee5\uff0cDOM \u5c42\u7ea7\u5173\u7cfb\u4f9d\u7136\u6309\u7167\u539f\u59cb\u7684 DOM \u5173\u7cfb\u5904\u7406\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4e0b\u9762\u4e24\u79cd\u5904\u7406\u90fd\u53ef\u4ee5\u5173\u95ed\u5f39\u6846\uff1a<\/p>\n<pre><span style=\"color:green\">\/\/ 1. \u5339\u914d\u6309\u94ae\u5143\u7d20\u6dfb\u52a0\u4e8b\u4ef6<\/span>\r\n&lt;zxx-alert open&gt;\r\n    &lt;p slot=\"alert\"&gt;\u63d2\u69fd\u6267\u884c\u6210\u529f\uff01&lt;\/p&gt;\r\n    &lt;button slot=\"button\" onclick=\"this.parentElement.removeAttribute('open')&gt;\u53d6\u6d88&lt;\/button&gt;\r\n&lt;\/zxx-alert&gt;<\/pre>\n<p>\u6216\u8005\u5728\u5b9a\u4e49 Web \u7ec4\u4ef6\u7684\u65f6\u5019\u5904\u7406\uff08\u53ea\u663e\u793a\u5173\u952e\u4ee3\u7801\uff09\uff1a<\/p>\n<pre>customElements.define('zxx-alert', class extends HTMLElement {\r\n    constructor() {\r\n        <span style=\"color:green\">\/\/ \u540c\u4e0a<\/span>\r\n    }\r\n    connectedCallback () {\r\n        <span style=\"color:green\">\/\/ \u76f4\u63a5 slot \u5143\u7d20\u4e0a\u6dfb\u52a0\u4e8b\u4ef6<\/span>\r\n        this.shadowRoot.querySelector('[name=\"button\"]').onclick = () => {\r\n            this.toggleAttribute('open', false);\r\n        };\r\n    }\r\n});<\/pre>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202109\/html-slot-dialog-demo.php\" rel=\"noopener\" target=\"_blank\">HTML slot\u5143\u7d20\u5b9e\u73b0\u5f39\u6846demo<\/a><\/p>\n<h3>\u516d\u3001\u7279\u6b8a\u7684display\u503c<\/h3>\n<p>\u636e\u6211\u6240\u77e5\uff0c<code>&lt;slot&gt;<\/code> \u5143\u7d20\u662f\u552f\u4e00 <code>display<\/code> \u9ed8\u8ba4\u503c\u662f <code>contents<\/code> \u7684\u5143\u7d20\uff08\u5982\u679c\u9519\u8bef\uff0c\u6b22\u8fce\u6307\u6b63\uff09\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202109\/2021-09-23_203317.png\" width=\"373\" height=\"165\" alt=\"slot\u5143\u7d20\u9ed8\u8ba4display\u503c\"><\/p>\n<h4>display:contents\u662f\u4ec0\u4e48\uff1f<\/h4>\n<p><code>display:contents<\/code> \u7684\u4f5c\u7528\u5c31\u662f\u5176\u540d\u5b57\u6240\u8868\u793a\u7684\u610f\u601d\uff0c\u53ea\u6709\u5185\u5bb9\u76d2\u5b50\uff0c\u5176\u4f59\u6240\u6709\u76d2\u5b50\u90fd\u53d6\u6d88\uff0c\u8fd9\u5c31\u4ea7\u751f\u4e86\u4e0b\u9762\u8fd9\u4e9b\u73b0\u8c61\uff1a<\/p>\n<ul>\n<li>\u6240\u6709\u7684\u5e03\u5c40\u90fd\u9700\u8981\u52a8\u7528\u76d2\u6a21\u578b\uff0c\u56e0\u6b64 <code>display:contents<\/code> \u5143\u7d20\u6ca1\u6709\u4efb\u4f55\u5e03\u5c40\u6548\u679c\uff1b\u4f8b\u5982\uff0c\u4e0b\u9762\u7684 <code>&lt;item&gt;<\/code> \u5143\u7d20\u867d\u7136\u88ab <code>&lt;div&gt;<\/code> \u5d4c\u5957\u4e86\uff0c\u4f46\u4f9d\u7136\u662f flex \u5b50\u9879\uff0c\u56e0\u4e3a\u8fd9\u5c42\u5d4c\u5957\u7684 <code>&lt;div&gt;<\/code> \u4e0d\u53c2\u4e0e\u5e03\u5c40\u3002\n<pre>&lt;section style=\"display:flex;height:100px;\"&gt;\r\n  &lt;div style=\"display:contents;\"&gt;\r\n    &lt;item style=\"flex:1;background:skyblue;\"&gt;1&lt;\/item&gt;\r\n    &lt;item style=\"flex:1;background:aliceblue;\"&gt;2&lt;\/item&gt;\r\n    &lt;item style=\"flex:1;background:skyblue;\"&gt;3&lt;\/item&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/section&gt;<\/pre>\n<p>\u6e32\u67d3\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202109\/2021-09-23_203030.png\" width=\"553\" height=\"117\" alt=\"display:contents\u4e0d\u53c2\u4e0e\u5e03\u5c40\">\n<\/li>\n<li>\u5bbd\u9ad8\u80cc\u666f\u7b49\u90fd\u662f\u4f5c\u7528\u5728\u975e\u5185\u5bb9\u76d2\u5b50\u4e0a\u7684\uff0c\u56e0\u6b64 <code>display:contents<\/code> \u5143\u7d20\u65e0\u6cd5\u8bbe\u7f6e\u5c3a\u5bf8\uff0c\u65e0\u6cd5\u8bbe\u7f6e\u80cc\u666f\u8272\uff1b<\/li>\n<li><code>display:contents<\/code> \u5143\u7d20\u53ea\u80fd\u8bbe\u7f6e\u4e0e\u6587\u5b57\u5185\u5bb9\u76f8\u5173\u7684CSS\uff0c\u4f8b\u5982color\u989c\u8272\uff0cfont-size\u5b57\u53f7\u7b49\u3002<\/li>\n<\/ul>\n<p>\u603b\u800c\u8a00\u4e4b\uff0c<code>display:contents<\/code>\u5c31\u662f\u201c\u5360\u7740\u8305\u5751\u4e0d\u62c9\u5c4e\u201d\u7684\u610f\u601d\uff0c\u5982\u679c\u5bf9\u8fd9\u4e2a CSS \u58f0\u660e\u7684\u4f5c\u7528\u6ca1\u6709\u591a\u5927\u6982\u5ff5\uff0c\u60f3\u60f3 <code>&lt;slot&gt;<\/code> \u5143\u7d20\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u975e\u672c\u6587\u91cd\u70b9\uff0c\u4e0d\u518d\u8fdb\u4e00\u6b65\u5c55\u5f00\u4e86\u3002<\/p>\n<h3>\u4e03\u3001\u7ed3\u5c3e\u518d\u8bf4\u70b9\u4ec0\u4e48<\/h3>\n<p>\u521a\u521a\u5c1d\u8bd5\u5f55\u4e86\u70b9 <a href=\"https:\/\/l-ui.com\/\" rel=\"noopener\" target=\"_blank\">LuLu UI<\/a> \u7684\u6559\u7a0b\u89c6\u9891\uff0c\u6548\u679c\u53ef\u4ee5\u8bf4\u60e8\u4e0d\u5fcd\u7779\u5f62\u5bb9\uff0c\u5149\u7ebf\uff0c\u80cc\u666f\u90fd\u4e0d\u5fcd\u76f4\u89c6\uff0c\u5f53\u7136\u8fd8\u6709\u8868\u8fbe\u548c\u9648\u8ff0\u7684\u95ee\u9898\uff0c\u770b\u6765\u5f55\u89c6\u9891\u4e5f\u662f\u95e8\u5f88\u6df1\u7684\u5b66\u95ee\uff0c\u6162\u6162\u79ef\u7d2f\u5427\u3002<\/p>\n<p><a href=\"https:\/\/bbs.cssworld.cn\/\" rel=\"noopener\" target=\"_blank\">CSS\u4e16\u754c\u8bba\u575b\u9996\u9875<\/a>\u524d\u4e24\u5929\u5f04\u4e86\u4e0b\uff0c\u770b\u8d77\u6765\u6709\u70b9\u6837\u5b50\u4e86\uff0c\u7167\u7740\u4e4b\u524d Discuz \u7684 UI \u5f04\u4e86\u4e0b\uff0c\u5176\u5b9e\u662f GitHub Issues \u7684\u6570\u636e\uff0c\u4e4b\u524d Discuz \u6709\u6f0f\u6d1e\uff0c\u7f51\u7ad9\u603b\u662f\u88ab\u653b\u51fb\uff0c\u6211\u7ed9\u4e0b\u6389\u4e86\uff0c\u8001\u6570\u636e\u6211\u4f1a\u6709\u7a7a\u7684\u65f6\u5019\u6574\u7406\u4e0a\u53bb\u7684\u3002<\/p>\n<p>\u5176\u4ed6\u5c31\u6ca1\u4ec0\u4e48\u60f3\u8bf4\u7684\u4e86\uff0c\u8fd9\u4e24\u5929\u7279\u522b\u7684\u56f0\uff0c\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\uff0c\u624d23:49\uff0c\u5c31\u56f0\u5f97\u4e0d\u884c\uff0c\u5f55\u89c6\u9891\u7684\u65f6\u5019\u773c\u775b\u4e5f\u662f\u4e0d\u505c\u5730\u7728\uff0c\u96be\u9053\u662f\u5c81\u6708\u4e0d\u9976\u4eba\u7684\u8868\u73b0\u3002<\/p>\n<p>\u5bf9\u4e86\uff0c\u60f3\u8d77\u6765\u4e86\uff0c\u8865\u5145\u4e0b\uff0c\u517c\u5bb9\u6027\u5fd8\u8bb0\u8bf4\u4e86\uff0c<code>&lt;slot&gt;<\/code> \u5143\u7d20\u517c\u5bb9\u6027\u548c Web \u7ec4\u4ef6\u5f00\u53d1 V2 \u89c4\u8303\u4e00\u81f4\uff0c\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u652f\u6301\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202109\/2021-09-24_000604.png\" width=\"632\" height=\"218\" alt=\"slot\u517c\u5bb9\u6027\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u8fd9\u56de\u771f\u7684\u7ed3\u5c3e\u4e86\uff0c\u5982\u679c\u4f60\u89c9\u5f97\u672c\u6587\u5199\u5f97\u8fd8\u4e0d\u9519\uff0c\u6b22\u8fce\u5206\u4eab\u5230\u670b\u53cb\u5708\uff0c\u5fae\u535a\u7b49\u3002<\/p>\n<p>\u6bd4\u5fc3\uff0c\u611f\u8c22\uff01<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/2600.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=10125\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=10125<\/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\/202109\/slot-cover.png\" width=\"161\" height=\"99\" alt=\"slot\u5360\u4f4d\u56fe\" class=\"alignright  size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u672c\u6587\u5e94\u8be5\u662f\u76ee\u524d\u6700\u6df1\u5165\u6700\u7ec6\u81f4\u7684\u4ecb\u7ecd HTML slot \u63d2\u69fd\u5143\u7d20\u7684\u6587\u7ae0\u4e86\uff0c\u5982\u679c\u60a8\u5bf9 Web \u7ec4\u4ef6\u5f00\u53d1\u611f\u5174\u8da3\uff0c\u5219\u672c\u6587\u5185\u5bb9\u4e0d\u5bb9\u9519\u8fc7\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1097],"tags":[1554,1394,1785,9,1556,1222],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10125"}],"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=10125"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/10125\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=10125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=10125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=10125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}