{"id":23,"date":"2024-06-07T03:26:30","date_gmt":"2024-06-07T03:26:30","guid":{"rendered":"http:\/\/blog.yikepi.com\/?p=23"},"modified":"2024-06-10T02:35:14","modified_gmt":"2024-06-10T02:35:14","slug":"css-%e4%b8%ad-has-%e7%9a%84%e5%a8%81%e5%8a%9b","status":"publish","type":"post","link":"https:\/\/www.yikepi.com\/index.php\/2024\/06\/07\/css-%e4%b8%ad-has-%e7%9a%84%e5%a8%81%e5%8a%9b\/","title":{"rendered":"CSS \u4e2d :has() \u7684\u5a01\u529b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"syntax\">\u53e5\u6cd5<\/h2>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u5728\u5143\u7d20\u4e2d\u627e\u5230\u5e76\u89e3\u91ca\u4e86\u6211\u4eec\u8981\u67e5\u627e\u7684\u5185\u5bb9\uff0c CSS<code>:has()<\/code>\u4f2a\u7c7b\u5c06\u5e2e\u52a9\u8bbe\u7f6e\u5143\u7d20\u7684\u6837\u5f0f\u3002\u8fd9\u5c31\u50cf\u8bf4\uff1a<strong><em>\u201c\u5982\u679c\u8fd9\u4e2a\u6846\u4e2d\u6709\u7279\u5b9a\u5185\u5bb9\uff0c\u90a3\u4e48\u5c31\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u8bbe\u7f6e\u6846\u7684\u6837\u5f0f\uff0c\u5e76\u4e14\u53ea\u80fd\u4ee5\u8fd9\u79cd\u65b9\u5f0f\u8bbe\u7f6e\u3002\u201d<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:has(&lt;direct-selector&gt;) {\n  <em>\/* ... *\/<\/em>\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong><em>\u201c\u5982\u679c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7684\u4efb\u4f55\u76f8\u5bf9\u9009\u62e9\u5668\u5728\u951a\u5b9a\u8be5\u5143\u7d20\u65f6\u4e0e\u81f3\u5c11\u4e00\u4e2a\u5143\u7d20\u5339\u914d\uff0c\u5219\u51fd\u6570\u5f0f<code>:has()<\/code>CSS \u4f2a\u7c7b\u8868\u793a\u8be5\u5143\u7d20\u3002\u6b64\u4f2a\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u901a\u8fc7\u5c06\u76f8\u5bf9\u9009\u62e9\u5668\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u6765\u9009\u62e9\u76f8\u5bf9\u4e8e\u53c2\u8003\u5143\u7d20\u7684\u7236\u5143\u7d20\u6216\u4e0a\u4e00\u4e2a\u540c\u7ea7\u5143\u7d20\u7684\u65b9\u6cd5\u3002\u201d<\/em><\/strong>\u5bf9\u4e8e\u66f4\u8be6\u5c3d\u7684\u89e3\u91ca\uff0c<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/:has\">MDN<\/a>\u505a\u5f97\u5f88\u5b8c\u7f8e<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-styling-problem\"><a href=\"https:\/\/css-tricks.com\/the-power-of-has-in-css\/#aa-the-styling-problem\"><\/a>\u9020\u578b\u95ee\u9898<\/h2>\n\n\n\n<p>\u5728\u8fc7\u53bb\u7684\u51e0\u5e74\u91cc\uff0c\u6211\u4eec\u65e0\u6cd5\u4f7f\u7528 CSS \u6839\u636e\u7236\u5143\u7d20\u7684\u76f4\u63a5\u5b50\u5143\u7d20\u6216\u57fa\u4e8e\u5176\u4ed6\u5143\u7d20\u7684\u5143\u7d20\u6765\u8bbe\u7f6e\u7236\u5143\u7d20\u7684\u6837\u5f0f\u3002\u5982\u679c\u5fc5\u987b\u8fd9\u6837\u505a\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u4e00\u4e9b JavaScript \u5e76\u6839\u636e HTML \u7ed3\u6784\u6253\u5f00\/\u5173\u95ed\u7c7b\u3002<code>:has()<\/code>\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n\n\n\n<p><br>\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u4e00\u7ea7\u6807\u9898\u5143\u7d20 (&nbsp;<code>h1<\/code>)\uff0c\u5b83\u662f\u535a\u5ba2\u5217\u8868\u9875\u9762\u4e0a\u7684\u5e16\u5b50\u6807\u9898\u6216\u7c7b\u4f3c\u5185\u5bb9\uff0c\u7136\u540e\u60a8\u6709\u4e00\u4e2a\u4e8c\u7ea7\u6807\u9898 (&nbsp;<code>h2<\/code>) \u7d27\u8ddf\u5728\u5176\u540e\u3002\u6b64 h2 \u53ef\u4ee5\u662f\u5e16\u5b50\u7684\u526f\u6807\u9898\u3002\u5982\u679c<code>h2<\/code>\u5b58\u5728\u3001\u91cd\u8981\u4e14\u7d27\u8ddf\u5728 \u4e4b\u540e<code>h1<\/code>\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u4f7f h1 \u7a81\u51fa\u3002\u4ee5\u524d\u60a8\u5fc5\u987b\u7f16\u5199 JS \u51fd\u6570\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"old-school-way-javascript\"><a href=\"https:\/\/css-tricks.com\/the-power-of-has-in-css\/#aa-old-school-way-javascript\"><\/a>\u4f20\u7edf\u65b9\u5f0f\u2014\u2014JavaScript<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>const h1Elements = document.querySelectorAll('h1');\n\nh1Elements.forEach((h1) =&gt; {\n  const h2Sibling = h1.nextElementSibling;\n  if (h2Sibling &amp;&amp; h2Sibling.tagName.toLowerCase() === 'h2') {\n    h1.classList.add('highlight-content');\n  }\n});\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a JS \u51fd\u6570\u6b63\u5728\u5bfb\u627e\u6240\u6709\u5e26\u6709\u524d\u7f00\u7684 h1&nbsp;<code>h2<\/code>\uff0c\u5e76\u5e94\u7528\u4e00\u4e2a highlight-content \u7c7b\u6765\u4f7f\u5176<code>h1<\/code>\u6210\u4e3a\u4e00\u7bc7\u91cd\u8981\u7684\u6587\u7ae0\u3002<\/p>\n\n\n\n<p>\u73b0\u4ee3 CSS \u7684\u5168\u65b0\u6539\u8fdb\u6b63\u5728\u706b\u70ed\u8fdb\u884c\u4e2d\uff01\u6211\u4eec\u5728\u6d4f\u89c8\u5668\u4e2d\u53ef\u4ee5\u6267\u884c\u7684\u64cd\u4f5c\u529f\u80fd\u5df2\u7ecf\u53d6\u5f97\u4e86\u957f\u8db3\u7684\u8fdb\u6b65\u3002\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u5229\u7528 CSS \u6765\u6267\u884c\u4f20\u7edf\u4e0a\u9700\u8981\u4f7f\u7528 JavaScript \u6267\u884c\u7684\u64cd\u4f5c\uff0c\u867d\u7136\u4e0d\u662f\u6240\u6709\u64cd\u4f5c\uff0c\u4f46\u53ef\u4ee5\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"new-school-way-css\"><a href=\"https:\/\/css-tricks.com\/the-power-of-has-in-css\/#aa-new-school-way-css\"><\/a>\u65b0\u5b66\u6821\u4e4b\u8def \u2013 CSS<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>h1:has(+ h2) {\n    color: blue;\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"throw-some-has-on-it\"><a href=\"https:\/\/css-tricks.com\/the-power-of-has-in-css\/#aa-throw-some-has-on-it\"><\/a><strong>\u52a0\u4e0a\u4e00\u4e9b :has()\uff01<\/strong><\/h2>\n\n\n\n<p>\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b83<code>:has()<\/code>\u6765\u5b9e\u73b0\u4e0e JS \u51fd\u6570\u76f8\u540c\u7684\u529f\u80fd\u3002\u6b64 CSS \u68c0\u67e5\u4efb\u4f55 h1\uff0c\u5e76\u4f7f\u7528<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/:has#with_the_sibling_combinator\">\u540c\u7ea7\u7ec4\u5408\u5668<\/a>\u68c0\u67e5\u7d27\u968f\u5176\u540e\u7684 h2\uff0c\u5e76\u5c06\u84dd\u8272\u6dfb\u52a0\u5230\u6587\u672c\u4e2d\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a<code>:has()<\/code>\u53ef\u4ee5\u6d3e\u4e0a\u7528\u573a\u7684\u7528\u4f8b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"has-selector-example-1\"><a href=\"https:\/\/css-tricks.com\/the-power-of-has-in-css\/#aa-has-selector-example-1\"><\/a>:has \u9009\u62e9\u5668\u793a\u4f8b 1<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"html\"><a href=\"https:\/\/css-tricks.com\/the-power-of-has-in-css\/#aa-html\"><\/a>HTML<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h1&gt;Lorem, ipsum dolor.&lt;\/h1&gt;\n&lt;h2&gt;Lorem ipsum dolor sit amet.&lt;\/h2&gt;\n&lt;p&gt;Lorem, ipsum dolor sit amet consectetur adipisicing elit. Eius, odio voluptatibus est vero iste ad?&lt;\/p&gt;\n\t\n<em>&lt;!-- WITHOUT HAS BELOW --&gt;<\/em>\n\n&lt;h1&gt;This is a test&lt;\/h1&gt;\n&lt;p&gt;Lorem, ipsum dolor sit amet consectetur adipisicing elit. Eius, odio voluptatibus est vero iste ad?&lt;\/p&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"css\"><a href=\"https:\/\/css-tricks.com\/the-power-of-has-in-css\/#aa-css\"><\/a>CSS<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>h1:has(+ h2) {\n    color: blue;\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2024\/04\/example-1.png?resize=808%2C470&amp;ssl=1\" alt=\"\u663e\u793a :has \u4e0e\u4f2a\u7c7b\u4e00\u8d77\u4f7f\u7528\u7684\u793a\u4f8b\u3002\" class=\"wp-image-377693\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"has-selector-example-2\"><a href=\"https:\/\/css-tricks.com\/the-power-of-has-in-css\/#aa-has-selector-example-2\"><\/a>:has \u9009\u62e9\u5668\u793a\u4f8b 2<\/h2>\n\n\n\n<p>\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u4f5c\u4e3a\u7f51\u7edc\u5de5\u4f5c\u8005\u90fd\u5728\u5904\u7406\u6216\u5904\u7406\u56fe\u50cf\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<a href=\"https:\/\/cloudinary.com\/\">Cloudinary<\/a>\u63d0\u4f9b\u7684\u5de5\u5177\u5bf9\u56fe\u50cf\u8fdb\u884c\u5404\u79cd\u8f6c\u6362\uff0c\u4f46\u901a\u5e38\u6211\u4eec\u60f3\u8981\u6dfb\u52a0\u9634\u5f71\u3001\u8fb9\u6846\u534a\u5f84\u548c\u6807\u9898\uff08\u4e0d\u8981\u4e0e alt \u5c5e\u6027\u4e2d\u7684\u66ff\u4ee3\u6587\u672c\u6df7\u6dc6\uff09\u3002<\/p>\n\n\n\n<p><br>\u4e0b\u9762\u7684\u4f8b\u5b50\u7528\u4e8e<code>:has()<\/code>\u67e5\u770b\u56fe\u5f62\u6216\u56fe\u50cf\u662f\u5426\u5177\u6709 figcaption \u5143\u7d20\uff0c\u5982\u679c\u6709\uff0c\u5219\u5e94\u7528\u4e00\u4e9b\u80cc\u666f\u548c\u8fb9\u6846\u534a\u5f84\u6765\u4f7f\u56fe\u50cf\u8131\u9896\u800c\u51fa\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"html\"><a href=\"https:\/\/css-tricks.com\/the-power-of-has-in-css\/#aa-html\"><\/a>HTML<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;section&gt;\n  &lt;figure&gt;\n    &lt;img src=\"https:\/\/placedog.net\/500\/280\" alt=\"My aunt sally's dog is a golden retreiver.\" \/&gt;\n    &lt;figcaption&gt;My Aunt Sally's Doggo&lt;\/figcaption&gt;\n  &lt;\/figure&gt;\n&lt;\/section&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"css\"><a href=\"https:\/\/css-tricks.com\/the-power-of-has-in-css\/#aa-css\"><\/a>CSS<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>figure:has(figcaption) {\n  background: #c3baba;\n  padding: 0.6rem;\n  max-width: 50%;\n  border-radius: 5px;\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/codepen.io\/chrisdemars\/pen\/oNOGqbK\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53e5\u6cd5 \u5982\u679c\u6211\u4eec\u5728\u5143\u7d20\u4e2d\u627e\u5230\u5e76\u89e3\u91ca\u4e86\u6211\u4eec\u8981\u67e5\u627e\u7684\u5185\u5bb9\uff0c CSS:has()\u4f2a\u7c7b\u5c06\u5e2e&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-css","wpcat-5-id"],"_links":{"self":[{"href":"https:\/\/www.yikepi.com\/index.php\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yikepi.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yikepi.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yikepi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yikepi.com\/index.php\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":1,"href":"https:\/\/www.yikepi.com\/index.php\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/www.yikepi.com\/index.php\/wp-json\/wp\/v2\/posts\/23\/revisions\/24"}],"wp:attachment":[{"href":"https:\/\/www.yikepi.com\/index.php\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yikepi.com\/index.php\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yikepi.com\/index.php\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}