aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/htmlorforeignelement/dataset/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/api/htmlorforeignelement/dataset/index.html')
-rw-r--r--files/zh-cn/web/api/htmlorforeignelement/dataset/index.html124
1 files changed, 124 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/htmlorforeignelement/dataset/index.html b/files/zh-cn/web/api/htmlorforeignelement/dataset/index.html
new file mode 100644
index 0000000000..bb7e0977e7
--- /dev/null
+++ b/files/zh-cn/web/api/htmlorforeignelement/dataset/index.html
@@ -0,0 +1,124 @@
+---
+title: HTMLElement.dataset
+slug: Web/API/HTMLOrForeignElement/dataset
+tags:
+ - HTMLElement.dataset
+translation_of: Web/API/HTMLOrForeignElement/dataset
+original_slug: Web/API/HTMLElement/dataset
+---
+<p>{{ APIRef }}</p>
+
+<p><code><strong>HTMLElement.dataset</strong></code>属性允许无论是在读取模式和写入模式下访问在 HTML或 DOM中的元素上设置的所有<a href="https://developer.mozilla.org/en/HTML/Global_attributes#attr-data-*">自定义数据属性</a>(<em>data-*</em>)集。</p>
+
+<p>它是一个<a href="/zh-CN/DOM/DOMStringMap">DOMString的映射</a>,每个自定义数据属性的一个条目。</p>
+
+<p>请注意,<strong>dataset </strong>属性本身可以被读取,但不能直接写入。相反,所有的写入必须是它的“属性”,这反过来表示数据属性。</p>
+
+<p>还要注意,一个HTML <code><strong>data-</strong></code><em>attribute </em>及其对应的DOM <code><strong>dataset.</strong></code><em>property</em> 不共享相同的名称,但它们总是相似的:</p>
+
+<ul>
+ <li>在HTML中的一个自定义数据属性的名称以 data- 开头。它只能包含字母,数字和以下字符: dash (<code>-</code>), dot (<code>.</code>), colon (<code>:</code>), underscore (<code>_</code>)  - 但不是任何ASCII大写字母(A到Z)。</li>
+ <li>JavaScript 中的一个自定义数据属性的名称是相同HTML属性的名称,但在 camelCase中,没有破折号,点等。</li>
+</ul>
+
+<p> </p>
+
+<p>自定义的数据属性名称是以 <code>data- </code>开头的。 它必须要遵循 <a class="external" href="http://www.w3.org/TR/REC-xml/#NT-Name" rel="external nofollow" title="http://www.w3.org/TR/REC-xml/#NT-Name">the production rule of xml names</a> 规则,该规则是说它只可以包含字母,数字和下面的字符: dash (<code>-</code>), dot (<code>.</code>), colon (<code>:</code>), underscore (<code>_</code>)。此外不应包含ASCII 码大写字母。</p>
+
+<p>自定义的data 属性名称转化成 {{ domxref("DOMStringMap") }} 的键值时会遵循下面的规则:</p>
+
+<ul>
+ <li>前缀  <code>data-</code> 被去除(包括减号);</li>
+ <li>对于每个在ASCII小写字母<span style="line-height: 1.5;"> </span><code style="font-style: normal;">a到</code><span style="line-height: 1.5;"> </span><code style="font-style: normal;">z前面</code>的减号 (<code>U+002D</code>),减号会被去除,并且字母会转变成对应的大写字母。</li>
+ <li>其他字符(包含其他减号)都不发生变化</li>
+</ul>
+
+<p>与此相反的转换,即将键值转换为一个属性的名称,会遵循下面的规则:</p>
+
+<ul>
+ <li>约束:减号在转变前一定不能紧跟一个ASCII小写字母 <code style="font-style: normal;">a</code> 到 <code style="font-style: normal;">z之间;</code></li>
+ <li>添加 <code>data-</code> 前缀;</li>
+ <li>任何ASCII大写字母 <code>A</code> 到 <code>Z</code> 将转化成一个减号紧跟对应的小写字母;</li>
+ <li>其他字符不会发生变化</li>
+</ul>
+
+<p>上面规则的约束是为了保证这两种转换是正好相反的转换。</p>
+
+<p>例如,属性名称 <code>data-abc-def</code> 与键值 <code>abcDef 相对应。</code></p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox"><em>string</em> = <em>element</em>.dataset.<em>camelCasedName</em>;
+<em>element.</em>dataset.<em>camelCasedName</em> = <em>string</em>;</pre>
+
+<h2 id="实例">实例</h2>
+
+<pre class="brush: js">&lt;div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth&gt;John Doe
+&lt;/div&gt;
+
+var el = document.querySelector('#user');
+
+// el.id == 'user'
+// el.dataset.id === '1234567890'
+// el.dataset.user === 'johndoe'
+// el.dataset.dateOfBirth === ''
+
+el.dataset.dateOfBirth = '1960-10-03'; // set the DOB.
+
+// 'someDataAttr' in el.dataset === false
+
+el.dataset.someDataAttr = 'mydata';
+// 'someDataAttr' in el.dataset === true
+</pre>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<p>{{ CompatibilityTable() }}</p>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>8</td>
+ <td>{{ CompatGeckoDesktop("6.0") }}</td>
+ <td>11</td>
+ <td>11.10</td>
+ <td>6</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>IE Mobile</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>---</td>
+ <td>---</td>
+ <td>---</td>
+ <td>---</td>
+ <td>---</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p> </p>