--- title: HTMLElement.dataset slug: Web/API/HTMLElement/dataset tags: - API - HTML DOM - HTMLElement - Tham khảo - Thuộc tính - dataset translation_of: Web/API/HTMLOrForeignElement/dataset ---
The dataset property on the {{domxref("HTMLElement")}} interface provides read/write access to all the custom data attributes (data-*) set on the element. This access is available both in HTML and within the DOM. It is a map of DOMString, one entry for each custom data attribute. Note that the dataset property itself can be read, but not directly written. Instead, all writes must be to the individual properties within the dataset, which in turn represent the data attributes. Note also that an HTML data-attribute and its corresponding DOM dataset.property do not share the same name, but they are always similar:
data-. It must contain only letters, numbers and the following characters: dash (-), dot (.), colon (:), underscore (_) -- but NOT any ASCII capital letters (A to Z).In addition to the information below, you'll find a how-to guide for using HTML data attributes in our article Using data attributes.
Từ dash-style (kiểu-gạch-nối) sang camelCase (bướuLạcĐà): Tên thuộc tính dữ liệu tùy biến được chuyển đổi thành khóa cho {{ domxref("DOMStringMap") }} dự trên quy định sau:
data- (cùng gạch nối);U+002D) theo sau chữ cái in thường ASCII, từ a tới z, sẽ bị loại bỏ và chữ cái đó sẽ chuyển đổi thành dạng in hoa;Từ camelCase (bướuLạcĐà) sang dash-style (kiểu-gạch-nối): Cách chuyển đổi ngược lại, dò theo khóa để tìm ra tên thuộc tính, bằng quy định sau:
a đến z) ở dạng in thường ASCII (trước khi biến đổi);data-;A tới Z sẽ được đổi thành gạch nối theo sau chữ cái dạng in thường tương ứng.Việc có rằng buộc trong quy định trên nhằm đảm bảo việc chuyển đổi có thể đảo ngược cho nhau.
Ví dụ, thuộc tính có tên data-abc-def sẽ có khóa tương ứng abcDef.
null sẽ được chuyển thành chuỗi "null".data- như trên.<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>John Doe</div>
const el = document.querySelector('#user');
// el.id == 'user'
// el.dataset.id === '1234567890'
// el.dataset.user === 'johndoe'
// el.dataset.dateOfBirth === ''
// gán dữ liệu cho thuộc tính data
el.dataset.dateOfBirth = '1960-10-03';
// Kết quả: el.dataset.dateOfBirth === 1960-10-03
delete el.dataset.dateOfBirth;
// Kết quả: el.dataset.dateOfBirth === undefined
// 'someDataAttr' in el.dataset === false
el.dataset.someDataAttr = 'mydata';
// Kết quả: 'someDataAttr' in el.dataset === true
| Thông số kỹ thuật | Trạng thái | Chú thích |
|---|---|---|
| {{SpecName('HTML WHATWG', "dom.html#dom-dataset", "HTMLElement.dataset")}} | {{Spec2('HTML WHATWG')}} | No change from latest snapshot, {{SpecName('HTML5.1')}} |
| {{SpecName('HTML5.1', "dom.html#dom-dataset", "HTMLElement.dataset")}} | {{Spec2('HTML5.1')}} | Snapshot of {{SpecName('HTML WHATWG')}}, no change from {{SpecName('HTML5 W3C')}} |
| {{SpecName('HTML5 W3C', "dom.html#dom-dataset", "HTMLElement.dataset")}} | {{Spec2('HTML5 W3C')}} | Snapshot of {{SpecName('HTML WHATWG')}}, initial definition. |
{{Compat("api.HTMLElement.dataset")}}
data-* của thuộc tính phổ thông (global).