data-*
全局属性
form a class of attributes called
自定义数据属性
, that allow proprietary information to be exchanged between the
HTML
及其
DOM
representation by scripts.
All such custom data are available via the
HTMLElement
interface of the element the attribute is set on. The
HTMLElement.dataset
property gives access to them.
*
may be replaced by any name following
the production rule of XML names
with the following restrictions:
xml
, whatever case is used for these letters;
U+003A
);
注意,
HTMLElement.dataset
特性为
DOMStringMap
, and the name of the custom data attribute
data-test-value
will be accessible via
HTMLElement.dataset.testValue
(or by
HTMLElement.dataset["
testValue
"]
) as any dash (
U+002D
) is replaced by the capitalization of the next letter, converting the name to camelcase.
By adding
data-*
attributes, even ordinary HTML elements can become rather complex and powerful program-objects. For example, a space-ship "
sprite
"
in a game could be a simple
<img>
element with a
class
attribute and several
data-*
attributes:
<img class="spaceship cruiserX3" src="shipX3.png" data-ship-id="324" data-weapons="laserI laserII" data-shields="72%" data-x="414354" data-y="85160" data-z="31940" onclick="spaceships[this.dataset.shipId].blasted()">
For a more in-depth tutorial about using HTML data attributes, see Using data attributes .
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'data-*' in that specification. |
实时标准 | No change from latest snapshot, HTML 5.1 |
|
HTML 5.1
The definition of 'data-*' in that specification. |
推荐 | 快照 HTML 实时标准 , no change from HTML5 |
|
HTML5
The definition of 'data-*' in that specification. |
推荐 | 快照 HTML 实时标准 ,初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
data-*
属性
|
Chrome 完整支持 Yes | Edge 完整支持 12 | Firefox 完整支持 Yes | IE 完整支持 Yes | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 Yes | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
完整支持
HTMLElement.dataset
property that allows to access and modify these values.
<a>
<abbr>
<acronym>
<address>
<applet>
<area>
<article>
<aside>
<audio>
<b>
<base>
<basefont>
<bdi>
<bdo>
<bgsound>
<big>
<blink>
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center>
<cite>
<code>
<col>
<colgroup>
<command>
<content>
<data>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir>
<div>
<dl>
<dt>
<element>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font>
<footer>
<form>
<frame>
<frameset>
<h1>
<head>
<header>
<hgroup>
<hr>
<html>
<i>
<iframe>
<image>
<img>
<input>
<ins>
<isindex>
<kbd>
<keygen>
<label>
<legend>
<li>
<link>
<listing>
<main>
<map>
<mark>
<marquee>
<menu>
<menuitem>
<meta>
<meter>
<multicol>
<nav>
<nextid>
<nobr>
<noembed>
<noframes>
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<picture>
<plaintext>
<pre>
<progress>
<q>
<rb>
<rp>
<rt>
<rtc>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<shadow>
<slot>
<small>
<source>
<spacer>
<span>
<strike>
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<template>
<textarea>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt>
<u>
<ul>
<var>
<video>
<wbr>
<xmp>
<input>
类型
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">