has-selector
它能做什么
这项技能查找用 JavaScript 根据子元素内容来改变父元素样式的代码。适用于希望使用更简单、基于 CSS 的样式的开发者。
适用场景
- ✓当你的脚本在容器内的输入框发生变化时为其添加类名的时候。
- ✓当你只为了根据内容来设置父元素样式而编写 JavaScript 的时候。
- ✓当你希望减少难以同步维护的样式代码的时候。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/has-selector/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 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.
热度
- GitHub 星标
- 75k
- 排名
- #2865
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。