dataset
只读特性在
HTMLOrForeignElement
interface provides read/write access to all the
自定义数据属性
(
data-
*
) set on the element.
This access is available both in HTML and within the DOM. It is a map of
DOMString
s (
DOMStringMap
) with 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-
属性
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.
dash-style
to
camelCase
conversion
A custom data attribute name is transformed to a key for the
DOMStringMap
entry with the following rules
data-
is removed (including the dash);
U+002D
) followed by an ASCII lowercase letter
a
to
z
, the dash is removed, and the letter is transformed into its uppercase counterpart;
camelCase
to
dash-style
conversion
The opposite transformation, which maps a key to an attribute name, uses the following rules:
a
to
z;
data-
is added;
A
to
Z
is transformed into a dash, followed by its lowercase counterpart;
The restriction in the rules above ensures that the two transformations are the inverse one of the other.
For example, the attribute named
data-abc-def
corresponds to the key
abcDef
.
element
.dataset.
keyname
element
.dataset[
keyname
]
in
operator
can be used to check whether a given attribute exists.
例如:
null
is converted into the string
"null"
.
delete
operator
.
const dataAttrMap = element.dataset
A
DOMStringMap
.
<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 === ''
// set the data attribute
el.dataset.dateOfBirth = '1960-10-03';
// Result: el.dataset.dateOfBirth === 1960-10-03
delete el.dataset.dateOfBirth;
// Result: el.dataset.dateOfBirth === undefined
// 'someDataAttr' in el.dataset === false
el.dataset.someDataAttr = 'mydata';
// Result: 'someDataAttr' in el.dataset === true
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'HTMLElement.dataset' in that specification. |
实时标准 | No change from latest snapshot, HTML 5.1 |
|
HTML 5.1
The definition of 'HTMLElement.dataset' in that specification. |
推荐 | 快照 HTML 实时标准 , no change from HTML5 |
|
HTML5
The definition of 'HTMLElement.dataset' in that specification. |
推荐 | 快照 HTML 实时标准 ,初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
dataset
|
Chrome 8 | Edge 12 | Firefox 6 | IE 11 | Opera 11 | Safari 5.1 | WebView Android 4.4 | Chrome Android 18 | Firefox Android 6 | Opera Android 11 | Safari iOS 5.1 | Samsung Internet Android 1.0 |
完整支持
api.HTMLOrForeignElement.dataset
after BCD is updated.
data-*
class of global attributes.
Element.getAttribute()
and
Element.setAttribute()
HTMLOrForeignElement
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers