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

event-delegation

Skill này làm gì

Skill này rà soát mã gắn nhiều trình lắng nghe sự kiện lên các mục trong danh sách hoặc hàng của bảng. Skill dành cho lập trình viên xây danh sách và trang động.

Khi nào nên dùng

  • ✓Khi bạn gắn cùng một trình xử lý click lên từng mục trong một danh sách dài.
  • ✓Khi các mục mới được thêm vào sau không phản hồi khi bấm.
  • ✓Khi một trang có nhiều trình lắng nghe làm dùng quá nhiều bộ nhớ.

Liên kết skill

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/event-delegation/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/event-delegation/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

“Bạn có thể đơn giản hóa các trình xử lý click cho danh sách này để các mục mới cũng hoạt động không?”

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

Cách hoạt động

Agent tìm cùng một trình xử lý được gắn cho nhiều phần tử con và gợi ý một trình lắng nghe duy nhất trên phần tử cha. Agent dùng closest để xác định mục đã được bấm.

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 event delegation for dynamic content

Adding hundreds of individual event listeners for list items or table rows consumes significant memory and creates performance overhead. Dynamic content (items loaded via AJAX or rendered after user actions) won't have event listeners unless you re-attach them after each update — event delegation solves both problems at once.

Quick Reference

  • Attach one listener to a parent instead of many to individual children
  • Use event.target.closest() to identify the intended target element
  • Event delegation automatically works for elements added dynamically
  • Clean up listeners on the parent when the container is removed

Check

Find any code in this file that attaches the same event handler to multiple elements in a loop. Suggest event delegation as an alternative.

Fix

Refactor these per-element event listeners to use event delegation on the common parent container.

Explain

Explain event delegation, how event bubbling enables it, and when to use closest() vs target.

Code Review

Review scripts, client components, and browser execution paths related to Use event delegation for dynamic content. Flag exact imports, event handlers, runtime side effects, or blocking operations that violate the rule, and state how the change should be verified in the browser.


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

Rule page: https://frontendchecklist.io/rules/javascript/event-delegation

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

Event delegation là gì?

Đó là lắng nghe trên một phần tử cha chung rồi kiểm tra phần tử con nào đã kích hoạt sự kiện, thay vì lắng nghe trên từng phần tử con.

Vì sao cách này giúp với nội dung mới?

Trình lắng nghe ở cha bắt được sự kiện từ bất kỳ phần tử con nào, kể cả các mục được thêm sau khi trang tải.

Closest được dùng để làm gì?

Skill dùng closest để tìm đúng phần tử mục tiêu, ngay cả khi lần bấm rơi vào một phần tử con bên trong nó.

Chi tiết

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

Use when reviewing scripts, client components, bundles, or runtime behavior related to Use event delegation for dynamic content. Inspect both source code and the browser execution path so fixes target the real bottleneck or bug.

repo: thedaviddias/Front-End-Checklist

Độ phổ biến

Sao GitHub
75k
Vị trí xếp hạng
#2837
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ự