Using data-* attribute
来源:互联网 发布:怎样对重复数据编号 编辑:程序博客网 时间:2024/06/05 13:30
原文地址:
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes
HTML5 is designed with extensibility for data that should be in the HTML, but not visible.data-* attributes allow us to store extra information on standard, semantic HTML elements without polluting the class name.
HTML Syntax
The syntax is easy. Say you have an article and you want to store some extra information that doesn’t have any visual representation. Just use data attributes for that:
<article id="electriccars" data-columns="3" data-indexnumber="12314" data-parent="cars">...</article>
JavaScript Access
Reading those out in JavaScript is also very simple. You could use getAttribute()
to read them but the standard defines a simpler way: aDOMStringMap
you can read out via a dataset
property:
var article = document.querySelector('#electriccars'), data = article.dataset; // data.columns -> "3"// data.indexnumber -> "12314"// data.parent -> "cars"
Each property is a string (even if you omit the quotes in the HTML) and can be read and written. In the above case settingarticle.dataset.columns = 5
would change that attribute.
CSS Access
Now, as data attributes are plain HTML attributes you can even access them fromCSS. For example to show the parent data on the article you can use generated content in CSS with the attr
function:
article::before { content: attr(data-parent);}
You can also use the attribute selectors in CSS to change styles according to the data:
article[data-columns='3']{ width: 400px;}article[data-columns='4']{ width: 600px;}
You can see all this working together in this JSBin example.
Data attributes can also be stored to contain information that is constantly changing, like scores in a game. Using the CSS selectors and JavaScript access here this allows you to build some nifty effects without having to write your own display routines. See the following screencast for an example using generated content and CSS transitions:
The code example shown in the screencast is also on JSBin.
Issues
Do not store content that should be visible and accessible in data attributes, because assistive technology may not access them. In addition, search crawlers do not index data attributes' values.
Sadly enough it seems there is nothing that is so simple and useful that doesn’t come with a price. In this case the main issues to consider are that Internet Explorerdoes not support the dataset
but you’d need to read them out withgetAttribute()
instead. The other issue is that the performance of reading data-attributes compared to storing this data in a JS data warehouse is bad. Usingdataset
is even slower than reading the data out with getAttribute()
.
That said, though, for content that is not to be shown they are a great solution and maybe we can get them into the next IE soon.
See also
- This article is adapted from Using data attributes in JavaScript and CSS on hacks.mozilla.org.
- How to use HTML5 data attributes (Sitepoint)
Document Tags and Contributors
preference: HTMLElement.dataset
The HTMLElement.dataset
property allows access, both in reading and writing mode, to all thecustom data attributes (data-*) set on the element. It is a map of DOMString, one entry for each custom data attribute.
The name of a custom data attribute begins with data-
. It must followthe production rule of xml names, which means that it may contain only letters, numbers and the following characters: dash (-
), dot (.
), colon (:
), underscore (_
). Moreover, it should not contain ASCII capital letters (A
to Z
).
A custom data attribute name is transformed to a key for the DOMStringMap
entry with the following rules
- the prefix
data-
is removed (including the dash); - for any dash (
U+002D
) followed by an ASCII lowercase lettera
toz
, the dash is removed and the letter is transformed into its uppercase counterpart; - other characters (including other dashes) are left unchanged.
The opposite transformation, that maps a key to an attribute name, uses the following rules:
- Restriction: A dash must not be immediately followed by an ASCII lowercase letter
a
toz
(before the transformation); - a prefix
data-
is added; - any ASCII uppercase letter
A
toZ
is transformed into a dash followed by its lowercase counterpart; - 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 keyabcDef
.
Syntax
string = element.dataset.camelCasedName;element.dataset.camelCasedName = string;
Examples
<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 === falseel.dataset.someDataAttr = 'mydata';// 'someDataAttr' in el.dataset === true
Browser compatibility
- Desktop
Mobile no support
- Using data-* attribute
- Using the jQuery Validate Plugin with HTML5 Data Attribute Rules
- Attribute meta-data
- Deleting Data Using SqlCommandBuilder
- Deleting Data Using SQLStatements
- Insert Data Using SqlCommandBuilder
- Update Data Using Transactions
- Using Data Table Mappings
- Using Data Copy
- Using Data and Variables
- Data rates using BLE
- data lab using zeppelin
- sort data using Python3
- Output an XML type attribute using DataContract
- Mass Update Item Attribute using interface
- AttributeError: 'Bunch' object has no attribute 'data'
- using OPENDATASOURCE Select Access Data Base data
- using OPENROWSET Select Access Data Base Data
- Python 函数式编程
- MavenDemo1
- java将一个图片进行旋转
- 基于软件开源实践(FLOSS)论共产主义的可实现性
- Linux下的MYSQL使用
- Using data-* attribute
- Android开发ToggleButton控件的学习
- comment style in C
- 冬天怎样美白祛斑
- Android 4.0设置Dialog点击屏幕不消失
- 什么情景下应当考虑使用JMS(Java Message Service )技术
- struts2.xml详解
- 数据库 到底猛在哪儿?
- python 函数装饰器学习