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:

  • In HTML, the name of a custom data attribute begins with 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 JavaScript, the name of a custom data attribute is the name of the same HTML attribute, but in camelCase and with no dashes, dots, etc.

In addition to the information below, you'll find a how-to guide for using HTML data attributes in our article Using data attributes.

Name conversion

dash-style to camelCase conversion

A custom data attribute name is transformed to a key for the DOMStringMap entry with the following rules

  1. The prefix data- is removed (including the dash);
  2. For any 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;
  3. Other characters (including other dashes) are left unchanged.
camelCase to dash-style conversion

The opposite transformation, which maps a key to an attribute name, uses the following rules:

  1. Restriction: Before the transformation, a dash 不必 be immediately followed by an ASCII lowercase letter a to z;
  2. The prefix data- is added;
  3. Any ASCII uppercase letter A to Z is transformed into a dash, followed by its lowercase counterpart;
  4. Other characters are left unchanged.

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 .

Accessing values

  • Attributes can be set and read by using the camelCase name (the key) like an object property of the dataset, as in element .dataset. keyname
  • Attributes can also be set and read using the bracket syntax, as in element .dataset[ keyname ]
  • in operator can be used to check whether a given attribute exists.

Setting values

  • When an attribute is set, its value is always converted into a string.
    • 例如: null is converted into the string "null" .

  • To remove an attribute, you can use the 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 实时标准 ,初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
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

图例

完整支持

完整支持

Please change the compat macro's paramter to api.HTMLOrForeignElement.dataset after BCD is updated.

另请参阅

元数据

  • 最后修改:
  1. HTMLOrForeignElement
  2. 特性
    1. dataset
    2. nonce
    3. tabIndex
  3. 方法
    1. blur()
    2. focus()
  4. HTML DOM 相关页面
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLAudioElement
    8. HTMLBRElement
    9. HTMLBaseElement
    10. HTMLBaseFontElement
    11. HTMLBodyElement
    12. HTMLButtonElement
    13. HTMLCanvasElement
    14. HTMLContentElement
    15. HTMLDListElement
    16. HTMLDataElement
    17. HTMLDataListElement
    18. HTMLDialogElement
    19. HTMLDivElement
    20. HTMLDocument
    21. HTMLElement
    22. HTMLEmbedElement
    23. HTMLFieldSetElement
    24. HTMLFormControlsCollection
    25. HTMLFormElement
    26. HTMLFrameSetElement
    27. HTMLHRElement
    28. HTMLHeadElement
    29. HTMLHeadingElement
    30. HTMLHtmlElement
    31. HTMLIFrameElement
    32. HTMLImageElement
    33. HTMLInputElement
    34. HTMLIsIndexElement
    35. HTMLKeygenElement
    36. HTMLLIElement
    37. HTMLLabelElement
    38. HTMLLegendElement
    39. HTMLLinkElement
    40. HTMLMapElement
    41. HTMLMediaElement
    42. HTMLMetaElement
    43. HTMLMeterElement
    44. HTMLModElement
    45. HTMLOListElement
    46. HTMLObjectElement
    47. HTMLOptGroupElement
    48. HTMLOptionElement
    49. HTMLOptionsCollection
    50. HTMLOutputElement
    51. HTMLParagraphElement
    52. HTMLParamElement
    53. HTMLPictureElement
    54. HTMLPreElement
    55. HTMLProgressElement
    56. HTMLQuoteElement
    57. HTMLScriptElement
    58. HTMLSelectElement
    59. HTMLShadowElement
    60. HTMLSourceElement
    61. HTMLSpanElement
    62. HTMLStyleElement
    63. HTMLTableCaptionElement
    64. HTMLTableCellElement
    65. HTMLTableColElement
    66. HTMLTableDataCellElement
    67. HTMLTableElement
    68. HTMLTableHeaderCellElement
    69. HTMLTableRowElement
    70. HTMLTableSectionElement
    71. HTMLTemplateElement
    72. HTMLTextAreaElement
    73. HTMLTimeElement
    74. HTMLTitleElement
    75. HTMLTrackElement
    76. HTMLUListElement
    77. HTMLUnknownElement
    78. HTMLVideoElement
    79. HashChangeEvent
    80. 历史
    81. ImageData
    82. 定位
    83. MessageChannel
    84. MessageEvent
    85. MessagePort
    86. Navigator
    87. NavigatorGeolocation
    88. NavigatorID
    89. NavigatorLanguage
    90. NavigatorOnLine
    91. NavigatorPlugins
    92. PageTransitionEvent
    93. Plugin
    94. PluginArray
    95. PopStateEvent
    96. PortCollection
    97. PromiseRejectionEvent
    98. RadioNodeList
    99. Transferable
    100. ValidityState
    101. Window
    102. WindowBase64
    103. WindowEventHandlers
    104. WindowTimers