Cho phép lưu trữ dữ liệu tùy chỉnh trên HTML element mà không vi phạm HTML spec.
- Cú pháp:
data-user-id="123". - Truy cập qua JS bằng
element.dataset.userId(tự convert kebab-case sang camelCase). Ứng dụng: truyền data từ HTML sang JS, CSS selectors ([data-active]), testing (data-testid). - React dùng data attributes để pass info cho DOM.
They allow storing custom data on HTML elements without violating the HTML spec.
- Syntax:
data-user-id="123". - Access via JS with
element.dataset.userId(automatically converts kebab-case to camelCase). - Uses: passing data from HTML to JS, CSS selectors (
[data-active]), testing hooks (data-testid). - React uses data attributes to pass info to the DOM.