Chuyển đến nội dung
Skillpedia
Website và giao diện (frontend)75k · #2865

has-selector

Skill này làm gì

Skill này tìm mã dùng JavaScript để thay đổi kiểu của phần tử cha dựa trên nội dung của các phần tử con. Skill dành cho lập trình viên muốn kiểu dáng đơn giản hơn, dựa trên CSS.

Khi nào nên dùng

  • ✓Khi script của bạn thêm một class vào một container mỗi khi một ô nhập bên trong nó thay đổi.
  • ✓Khi bạn chỉ viết JavaScript để định kiểu một phần tử cha dựa trên nội dung của nó.
  • ✓Khi bạn muốn bớt mã định kiểu khó giữ đồng bộ.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/has-selector/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Tôi có thể dùng CSS thay vì JavaScript để định kiểu cho phần tử cha khi phần tử con thay đổi không?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent tìm JavaScript đang bật tắt class của phần tử cha và gợi ý một bộ chọn :has nhắm vào phần tử cha khi điều kiện của con được thỏa mãn. Agent cũng giải thích :has khác :is và :not thế nào.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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

Câu hỏi thường gặp

:has làm gì?

Nó chọn một phần tử khi có một phần tử con hoặc hậu duệ khớp bên trong nó, điều mà CSS trước đây không làm được.

Nó có được hỗ trợ rộng rãi không?

Skill nói :has đã có hỗ trợ baseline trên mọi trình duyệt lớn từ cuối năm 2023.

Nó có thể làm trang chậm đi không?

Skill cảnh báo không nên dùng bộ chọn :has phức tạp ở các vùng bố cục được tính lại thường xuyên.

Chi tiết

Mô tả gốc của tác giả

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

Độ phổ biến

Sao GitHub
75k
Vị trí xếp hạng
#2865
Giấy phép
MIT
Cập nhật
31/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự