


HTML5 custom attribute data-* detailed introduction and JS operation examples_html5 tutorial skills
Of course, definition and data access can be done through scripts in advanced browsers. Very useful in project practice.
For example:
Use attribute method to access the value of data-* custom attribute
It is very convenient to use the attributes method to access the value of data-* custom attributes:
// Use getAttribute to get the data- attribute
var user = document . getElementById ( 'user' ) ;
var userName = plant . getAttribute ( 'data-uname' ) ; // userName = 'Script Home'
var userId = plant . getAttribute ( 'data-uid' ) ; // userId = '12345'
// Use setAttribute to set the data- attribute
user . setAttribute ( 'data-site' , 'http://www.jb51.net' ) ;
This method can work normally in all modern browsers, but it is not the purpose of HTML 5's custom data-* attributes, otherwise it will be no different from the custom attributes we used before, for example:
<script><br> // Use getAttribute to get the data- attribute<br> var user = document . getElementById ( 'user' ) ;<br> var userName = plant . getAttribute ( 'uname' ) ; // userName = 'Script Home'<br> var userId = plant . getAttribute ( 'uid' ) ; // userId = '12345'<br> <br> // Use setAttribute to set the data- attribute <br> user . setAttribute ( 'site' , 'http://www.jb51.net' ) ;<br> </script>
This "original" custom attribute is no different from the data-* custom attribute above, but the knowledge attribute name is different.
dataset attribute access data-*custom attribute value
This method accesses the value of data-* custom attributes by accessing the dataset attribute of an element. The dataset attribute is part of the HTML5 JavaScript API and is used to return a DOMStringMap object with the data- attributes of all selected elements.
When using this method, instead of using the complete attribute name, such as data-uid, to access data, the data- prefix should be removed.
Another thing to note is that if the data- attribute name contains a hyphen, for example: data-date-of-birth, the hyphen will be removed and converted to camel case naming. The previous attribute name will be converted After that it should be: dateOfBirth.
If you want to delete a data-attribute, you can do this: delete el . dataset . id ; or el .dataset . id = null ; .
Looks beautiful, haha, but unfortunately, the new dataset attribute is only implemented in Chrome 8 Firefox (Gecko) 6.0 Internet Explorer 11 Opera 11.10 Safari 6 browsers, so in the meantime it is best to use getAttribute and setAttribute to operate.
About data-attribute selector
In actual development, you may find it useful to select relevant elements based on custom data- attributes. For example, use querySelectorAll to select elements:
// Select all elements containing the 'data-flowering' attribute
document . querySelectorAll ( '[data-flowering]' ) ;
// Select all elements containing the 'data-text-colour' attribute value of red
document . querySelectorAll ( '[data-text-colour="red"]' ) ;
Similarly, we can also set CSS styles for corresponding elements through the data- attribute value, such as the following example:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
