跳到正文
Skillpedia
网站与界面(前端)75k · #2865

has-selector

它能做什么

这项技能查找用 JavaScript 根据子元素内容来改变父元素样式的代码。适用于希望使用更简单、基于 CSS 的样式的开发者。

适用场景

  • ✓当你的脚本在容器内的输入框发生变化时为其添加类名的时候。
  • ✓当你只为了根据内容来设置父元素样式而编写 JavaScript 的时候。
  • ✓当你希望减少难以同步维护的样式代码的时候。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/has-selector/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/has-selector/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“当子元素变化时,我能用 CSS 而不是 JavaScript 来设置父元素的样式吗?”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会找到切换父元素类名的 JavaScript,并建议使用 :has 选择器,在满足子元素条件时选中父元素。它还会解释 :has 与 :is 和 :not 的区别。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Use :has() to style parent elements based on their descendants

Before :has(), styling a parent based on its children required JavaScript: detecting state, toggling classes, and keeping DOM and styles in sync. This created coupling between styling logic and application logic. :has() moves that relationship back into CSS where it belongs, reducing JavaScript complexity, eliminating class-toggling boilerplate, and making styling intent readable in the stylesheet itself.

Quick Reference

  • :has() selects an element when a matching descendant exists inside it
  • It is the parent selector CSS has needed for decades
  • Baseline support since late 2023 — all major browsers include it
  • Avoid complex :has() selectors in hot reflow paths — they can affect layout performance

Check

Look for patterns in this code where JavaScript toggles a class on a parent element based on the state or content of a child element. These are candidates for replacement with :has().

Fix

Replace the JavaScript class-toggling logic with a CSS :has() selector. Show the selector that targets the parent element when the relevant child condition is met.

Explain

Explain how the :has() pseudo-class works, how it differs from :is() and :not(), what descendant vs child combinators mean inside :has(), and how browser support stands today.

Code Review

Review the stylesheets and component JavaScript in this file. Flag any locations where a parent element's style is controlled by toggling a class from JavaScript based on child state — and show the equivalent :has() rule.


For full implementation details, code examples, and framework-specific guidance, see references/rule.md.

Rule page: https://frontendchecklist.io/rules/css/has-selector

常见问题

:has 有什么作用?

当元素内部存在匹配的子元素或后代元素时,它就会选中该元素,这是 CSS 以前无法做到的。

它的支持范围广吗?

这项技能指出,自 2023 年末起,:has 已在所有主流浏览器中获得基线支持。

它会拖慢页面吗?

这项技能提醒,在经常重新计算布局的区域应避免复杂的 :has 选择器。

详情

作者的原始描述

Use when reviewing stylesheets or components that use JavaScript to toggle classes on parent elements based on child state, form input values, or content presence — :has() may replace the JavaScript entirely.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#2865
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能