{"id":11897,"date":"2025-11-17T15:48:36","date_gmt":"2025-11-17T07:48:36","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11897"},"modified":"2025-11-17T15:48:36","modified_gmt":"2025-11-17T07:48:36","slug":"css-state-function","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2025\/11\/css-state-function\/","title":{"rendered":"\u4ecb\u7ecd\u4e0b\u4e0eCSS\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u76f8\u5173\u7684:state()\u51fd\u6570"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11897\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11897<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0cAI\u6293\u53d6\u4fdd\u7559\u539f\u6587\u5730\u5740\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<h3>\u4e00\u3001\u4f60\u9700\u8981\u63d0\u524d\u77e5\u9053\u7684<\/h3>\n<p>Web Components\u7ec4\u4ef6\u5f00\u53d1\u53c8\u65b0\u589e\u65b0\u7279\u6027\uff0c\u4e3a<code>:state()<\/code>\u4f2a\u7c7b\u51fd\u6570\u3002<\/p>\n<p>4\u5e74\u591a\u524d\uff0c\u6709\u4ecb\u7ecd\u8fc7<code>::part()<\/code>\u4f2a\u5143\u7d20\u51fd\u6570\uff0c\u53ef\u4ee5\u7a7f\u900f\u7ec4\u4ef6\u5185\u90e8\uff0c\u5bf9\u91cc\u9762\u7684\u6837\u5f0f\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n<p>\u8be6\u89c1\u8fd9\u7bc7\u6587\u7ae0\uff1a\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/02\/css-part-shadow-dom\/\">\u4f7f\u7528::part\u4f2a\u5143\u7d20\u6539\u53d8Shadow DOM\u7684CSS\u6837\u5f0f<\/a>\u201d<\/p>\n<p>\u800c<code>:state()<\/code>\u4f2a\u7c7b\u51fd\u6570\uff0c\u5219\u662f\u7a7f\u900f\u7ec4\u4ef6\u5185\u90e8\uff0c\u5339\u914d\u72b6\u6001\u8fdb\u884c\u6837\u5f0f\u8bbe\u7f6e\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u7406\u89e3\u4e3a\uff0c<code>::part()<\/code>\u4f2a\u5143\u7d20\u51fd\u6570\u66f4\u50cf\u662f [disabled] \u8fd9\u79cd\u5c5e\u6027\u9009\u62e9\u5668\uff0c\u9700\u8981\u5185\u90e8HTML\u6709\u5bf9\u5e94\u7684\u5c5e\u6027\u8bbe\u7f6e\uff08\u901a\u8fc7\u8bbe\u7f6epart\u5c5e\u6027\uff09\u624d\u80fd\u5339\u914d\uff0c\u4f46\u662f<code>:state()<\/code>\u4f2a\u7c7b\u51fd\u6570\u66f4\u50cf\u662f <code>:disabled<\/code> \u8fd9\u7c7b\u4f2a\u7c7b\u9009\u62e9\u5668\uff0c\u770b\u4e2d\u7684\u662f\u5143\u7d20\u7684\u771f\u5b9e\u72b6\u6001\uff0c\u800c\u975e\u662f\u5426\u6709\u8fd9\u4e2a\u5c5e\u6027\u3002<\/p>\n<h4>attachInternals\u65b9\u6cd5<\/h4>\n<p>\u90a3\u5982\u4f55\u7ed9\u7ec4\u4ef6\u8bbe\u7f6e\u72b6\u6001\uff0c\u8ba9<code>:state()<\/code>\u4f2a\u7c7b\u51fd\u6570\u5339\u914d\u5462\uff1f\u8fd9\u4e2a\u5c31\u9700\u8981\u7528\u5230\u4eca\u5e74\u5e74\u521d\u4ecb\u7ecd\u7684<code>attachInternals<\/code>\u65b9\u6cd5\u3002<\/p>\n<p>\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u91cc\u4e86\u89e3\uff1a\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2025\/03\/html-element-elementinternals-attachinternals\/\">\u7814\u7a76\u4e0battachInternals\u65b9\u6cd5\uff0c\u53ef\u8ba9\u666e\u901a\u5143\u7d20\u6709\u8868\u5355\u7279\u6027<\/a>\u201d<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>const someInternals = someCustomElement.attachInternals() <\/pre>\n<p>\u800c\u8fd9\u4e2a<code>someInternals<\/code>\u5c31\u662f\u4e2a<code>ElementInternals<\/code>\u5bf9\u8c61\uff0c\u5176\u5305\u542b\u4e00\u4e2a\u540d\u4e3a<code>states<\/code>\u7684\u5c5e\u6027\uff08\u89c1\u4e0b\u56fe\uff09\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202511\/2025-11-17_102830.png\" width=\"519\" height=\"93\" alt=\"status\u8bed\u6cd5\" class=\"alignnone size-medium imgpad\" \/><\/p>\n<p><code>states<\/code>\u5c5e\u6027\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a<code>CustomStateSet<\/code>\u5bf9\u8c61\uff0c\u5305\u542b\u4ee5\u4e0b\u4e00\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n<pre>\/\/ \u5c5e\u6027\r\nCustomStateSet.size<\/pre>\n<pre>\/\/ \u65b9\u6cd5\r\nCustomStateSet.add()\r\nCustomStateSet.clear()\r\nCustomStateSet.delete()\r\nCustomStateSet.entries()\r\nCustomStateSet.forEach()\r\nCustomStateSet.has()\r\nCustomStateSet.keys()\r\nCustomStateSet.values()<\/pre>\n<p>\u6211\u4eec\u65e5\u5e38\u5f00\u53d1\uff0c\u7528\u7684\u6bd4\u8f83\u591a\u7684\u8bb2\u5c31\u662f<code>add()<\/code>\u65b9\u6cd5\u6dfb\u52a0\u72b6\u6001\uff0c<code>delete()<\/code>\u65b9\u6cd5\u5220\u9664\u72b6\u6001\u3002<\/p>\n<h3>\u4e8c\u3001:state()\u51fd\u6570\u6848\u4f8b<\/h3>\n<p>\u4e00\u4f8b\u80dc\u5343\u8a00\uff0c\u8ba9\u6211\u60f3\u60f3\uff0c\u5f04\u4e2a\u4ec0\u4e48\u6848\u4f8b\u597d\u5462\u2026\u2026\u8981\u7ecf\u5178\uff0c\u53c8\u8981\u5177\u6709\u4ee3\u8868\u6027\uff0c\u55ef\u2026\u2026\u7b97\u4e86\uff0c\u968f\u4fbf\u793a\u610f\u4e0b\u6548\u679c\u5427\u3002<\/p>\n<p>JS\u548cCSS\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>class UiLoading extends HTMLElement {\r\n  constructor() {\r\n    super();\r\n\r\n    const internals = this.attachInternals();\r\n    \r\n    this.addEventListener('click', () =&gt; {\r\n      internals.states.add('loading');\r\n    \r\n      setTimeout(() =&gt; {\r\n        internals.states.delete('loading');\r\n      }, 3000);\r\n    });\r\n    \r\n  }\r\n\r\n  connectedCallback() {\r\n    this.click();\r\n  }\r\n}\r\n\/\/ \u5b9a\u4e49\u81ea\u5b9a\u4e49\u7ec4\u4ef6\r\nif (!customElements.get('ui-loading')) {\r\n  customElements.define('ui-loading', UiLoading);\r\n}<\/pre>\n<pre>ui-loading {\r\n  display: inline-grid;\r\n  place-items: center;\r\n  width: 150px; height: 150px;\r\n  border: 2px dashed;\r\n}\r\nui-loading:state(loading) {\r\n  border-color: red;\r\n  background: lightgreen;\r\n}<\/pre>\n<p>\u6b64\u65f6\uff0c\u53ea\u8981\u9875\u9762\u4e0a\u6709 <code>&lt;ui-loading&gt;<\/code> \u5143\u7d20\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u6837\u5f0f\u53d8\u5316\u4e86\u3002<\/p>\n<p>\u9ed8\u8ba4\u662f\u7eff\u5e95\u7ea2\u6846\uff0c\u51e0\u79d2\u949f\u65f6\u5019\uff0c\u5c31\u662f\u9ed1\u6846\u4e86\u3002<\/p>\n<p>\u5b9e\u65f6\u6548\u679c\u5982\u4e0b\u6240\u793a\uff0c\u5982\u679c\u770b\u4e0d\u5230\u6548\u679c\uff0c\u8bf7\u70b9\u51fb\u4e0b\u9762\u7684\u6846\u6846\u3002<\/p>\n<style>ui-loading {display: inline-grid;place-items: center;width: 150px;height: 150px;border: 2px dashed;}ui-loading:state(loading) {border-color: red;background: lightgreen;}<\/style>\n<p><script>class UiLoading extends HTMLElement {constructor() {super();const internals = this.attachInternals();this.addEventListener('click', () => {internals.states.add('loading');setTimeout(() => {internals.states.delete('loading');}, 3000);});}connectedCallback() {this.click();}}if (!customElements.get('ui-loading')) {customElements.define('ui-loading', UiLoading);}<\/script><\/p>\n<p><ui-loading><\/ui-loading><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<code>:state()<\/code>\u51fd\u6570\u7684\u8bbe\u8ba1\u521d\u8877\u5c31\u662f\u4e3a\u4e86\u65b9\u4fbf\u66b4\u9732\u7ec4\u4ef6\u5185\u90e8\u7684\u72b6\u6001\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u9009\u4e2d\u4e0e\u5426\uff0c\u5931\u8d25\u4e0e\u5426\uff0c\u52a0\u8f7d\u4e0e\u5426\u7b49\u3002<\/p>\n<h3>\u4e09\u3001\u517c\u5bb9\u6027\u4e0e\u7ed3\u8bed<\/h3>\n<p>CSS <code>:state()<\/code>\u4f2a\u7c7b\u51fd\u6570\u57282024\u5e745\u6708\u4efd\u7684\u65f6\u5019\uff0c\u5df2\u7ecf\u88ab\u6240\u6709\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u652f\u6301\uff0c\u5927\u89c4\u6a21\u5e94\u7528\u7684\u65f6\u673a\u8fd8\u4e0d\u6210\u719f\uff0c\u4e0d\u8fc7\u4e0d\u5f97\u4e0d\u8bf4\uff0c\u8fd9\u4e2a\u4e1c\u897f\u53ef\u4ee5\u63d0\u9ad8\u7ec4\u4ef6\u5f00\u53d1\u7684\u6863\u6b21\u611f\uff08\u867d\u7136<code>part<\/code>\u5c5e\u6027\u548c<code>::part()<\/code>\u51fd\u6570\u4e5f\u80fd\u5b9e\u73b0\u7c7b\u4f3c\u6548\u679c\uff0c\u4f46\u6ca1\u6709\u8fd9\u4e2a\u770b\u8d77\u6765\u9ad8\u5927\u4e0a\uff09\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202511\/2025-11-17_144048.png\" width=\"573\" height=\"353\" alt=\":state()\u51fd\u6570\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u603b\u7ed3<\/h4>\n<p>:state() \u4f2a\u7c7b\u662f\u672a\u6765 Web \u7ec4\u4ef6\u6837\u5f0f\u5316\u7684\u4e00\u4e2a\u5f3a\u5927\u4e14\u8bed\u4e49\u5316\u7684\u5de5\u5177\u3002\u5b83\u5c06\u6837\u5f0f\u63a7\u5236\u7684\u903b\u8f91\u4ece\u201c\u5982\u4f55\u6e32\u67d3\u201d\uff08\u66b4\u9732\u5185\u90e8\u90e8\u4ef6\uff09\u8f6c\u53d8\u4e3a\u201c\u5728\u4ec0\u4e48\u72b6\u6001\u4e0b\u6e32\u67d3\u201d\uff0c\u4f7f\u5f97\u7ec4\u4ef6\u5f00\u53d1\u8005\u80fd\u66f4\u597d\u5730\u5c01\u88c5\u5185\u90e8\u7ed3\u6784\uff0c\u540c\u65f6\u4e3a\u7ec4\u4ef6\u4f7f\u7528\u8005\u63d0\u4f9b\u6e05\u6670\u3001\u5f3a\u5927\u7684\u6837\u5f0f\u5b9a\u5236\u80fd\u529b\u3002<\/p>\n<h4>\u5410\u69fd<\/h4>\n<p>\u6628\u5929\u9047\u5230\u4e2a\u96be\u53d7\u7684\u4e8b\u60c5\uff0c\u6211\u9c7c\u7aff\u5305\u91cc\u7ffb\u6765\u8986\u53bb\uff0c\u6211\u7684\u4e00\u6839\u9c7c\u7aff\u4e0d\u89c1\u4e86\uff0c\u8fbe\u4ebf\u74e6\u4e00\u51fb\u67ab\uff0c\u662f\u6211\u4e70\u7684\u4ef7\u683c\u6bd4\u8f83\u9ad8\u7684\u6746\u5b50\u4e4b\u4e00\uff0c\u7528\u4e86\u5f88\u591a\u5e74\u4e86\uff0c\u4e5f\u6bd4\u8f83\u6709\u611f\u60c5\u4e86\u3002<\/p>\n<p>\u601d\u6765\u60f3\u53bb\uff0c\u662f\u4e0a\u5468\u5728\u60c5\u4eba\u8c37\u5782\u9493\u56ed\u9493\u9cab\u9c7c\u7684\u65f6\u5019\uff0c\u6ca1\u6709\u53ca\u65f6\u6536\u8d77\u6765\uff0c\u6401\u5728\u4e86\u6cb3\u8fb9\uff0c\u8d70\u7684\u65f6\u5019\uff0c\u53ea\u6536\u4e86\u53e6\u5916\u4e00\u53ea\u6746\u5b50\uff0c\u540c\u65f6\u635f\u5931\u7684\u8fd8\u6709\u4e00\u53ea\u6d6e\u6f02\u3002<\/p>\n<p>\u597d\u96be\u53d7\uff0c\u9493\u9c7c\u8fd9\u4e48\u591a\u5e74\uff0c\u8fd8\u662f\u5934\u4e00\u6b21\u4e22\u6746\u5b50\u3002<\/p>\n<p>\u6253\u7535\u8bdd\u7ed9\u8001\u677f\uff0c\u8001\u677f\u4e5f\u6ca1\u89c1\u5230\uff0c\u4f30\u8ba1\u88ab\u4eba\u6361\u8d70\u4e86\u3002<\/p>\n<p>\u90c1\u95f7\uff01<\/p>\n<p>\u9c7c\u6ca1\u6709\u9493\u5230\u51e0\u6761\uff0c\u6746\u5b50\u6ca1\u4e86\uff0c\u6211\u90fd\u4e0d\u6562\u544a\u8bc9\u5bb6\u91cc\u4eba\uff0c\u4e0d\u7136\u53c8\u8981\u5410\u69fd\u4e22\u4e09\u843d\u56db\uff0c\u53ea\u80fd\u6084\u6084\u53c8\u4e70\u4e86\u4e2a\u65b0\u6746\u5b50\u3002<\/p>\n<p>\u820d\u4e0d\u5f97\u4e70\u8d35\u7684\uff0c\u5c31\u4e70\u4e86\u4e2a200\u4e0d\u5230\u7684\uff0c\u5509\u2026\u2026<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202511\/2025-11-17_150107.jpeg\" width=\"146\" height=\"165\" alt=\"\u54ed\u6ce3\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=11897\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=11897<\/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\/202511\/2025-11-17_154426.jpeg\" width=\"160\" height=\"113\" alt=\":state()\u4f2a\u7c7b\u51fd\u6570\u5c01\u9762\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>Web Components\u7ec4\u4ef6\u5f00\u53d1\u53c8\u65b0\u589e\u7279\u6027\uff0c\u53ef\u4ee5\u66b4\u9732\u7ec4\u4ef6\u5185\u90e8\u7684\u72b6\u6001\uff0c\u901a\u8fc7CSS :state()\u4f2a\u7c7b\u51fd\u6570\u5339\u914d\uff0c\u589e\u5f3a\u7ec4\u4ef6\u7684\u6837\u5f0f\u5b9a\u5236\u80fd\u529b\uff0c\u4ee5\u5b9e\u73b0\u66f4\u597d\u7684\u5185\u90e8\u5c01\u88c5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[2088,386,1591,1005,1223],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11897"}],"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=11897"}],"version-history":[{"count":7,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11897\/revisions"}],"predecessor-version":[{"id":11961,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/11897\/revisions\/11961"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=11897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=11897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=11897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}