From 68b6aba0b2a1f8005e1c181c06e40eea4af9cd2f Mon Sep 17 00:00:00 2001 From: MDN Date: Sun, 4 Jul 2021 00:36:02 +0000 Subject: [CRON] sync translated content --- .../api/htmlorforeignelement/dataset/index.html | 124 +++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 files/zh-cn/orphaned/web/api/htmlorforeignelement/dataset/index.html (limited to 'files/zh-cn/orphaned') diff --git a/files/zh-cn/orphaned/web/api/htmlorforeignelement/dataset/index.html b/files/zh-cn/orphaned/web/api/htmlorforeignelement/dataset/index.html new file mode 100644 index 0000000000..6607ae59b3 --- /dev/null +++ b/files/zh-cn/orphaned/web/api/htmlorforeignelement/dataset/index.html @@ -0,0 +1,124 @@ +--- +title: HTMLElement.dataset +slug: orphaned/Web/API/HTMLOrForeignElement/dataset +tags: + - HTMLElement.dataset +translation_of: Web/API/HTMLOrForeignElement/dataset +original_slug: Web/API/HTMLOrForeignElement/dataset +--- +

{{ APIRef }}

+ +

HTMLElement.dataset属性允许无论是在读取模式和写入模式下访问在 HTML或 DOM中的元素上设置的所有自定义数据属性(data-*)集。

+ +

它是一个DOMString的映射,每个自定义数据属性的一个条目。

+ +

请注意,dataset 属性本身可以被读取,但不能直接写入。相反,所有的写入必须是它的“属性”,这反过来表示数据属性。

+ +

还要注意,一个HTML data-attribute 及其对应的DOM dataset.property 不共享相同的名称,但它们总是相似的:

+ + + +

 

+ +

自定义的数据属性名称是以 data- 开头的。 它必须要遵循 the production rule of xml names 规则,该规则是说它只可以包含字母,数字和下面的字符: dash (-), dot (.), colon (:), underscore (_)。此外不应包含ASCII 码大写字母。

+ +

自定义的data 属性名称转化成 {{ domxref("DOMStringMap") }} 的键值时会遵循下面的规则:

+ + + +

与此相反的转换,即将键值转换为一个属性的名称,会遵循下面的规则:

+ + + +

上面规则的约束是为了保证这两种转换是正好相反的转换。

+ +

例如,属性名称 data-abc-def 与键值 abcDef 相对应。

+ +

语法

+ +
string = element.dataset.camelCasedName;
+element.dataset.camelCasedName = string;
+ +

实例

+ +
<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>John Doe
+</div>
+
+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
+
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support8{{ CompatGeckoDesktop("6.0") }}1111.106
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support---------------
+
+ +

 

-- cgit v1.2.3-54-g00ecf