HTML5’s data-*custom attribute
HTML5 adds a new feature which is the custom data attribute, which is the data-*custom attribute. In HTML5, we can use data- as the prefix to set the custom attributes we need to store some data. Of course, definition and data access can be carried out through scripts in advanced browsers. Very useful in project practice. There are currently many frameworks that adopt this approach, and the most common one is jQueryMobile.
Specific usage examples are as follows:
<p id = "head" data-home = "http://blog.csdn.net/xmtblog" data-author = "伪专家"></p>
In the traditional approach, we can use it with jquery, as follows:
$("#head").attr("data-home"); $("#head").attr("data-home","new");
Or pure js approach:
In IE browser, We can just call it directly after getting the object
document.getElementById("head").["data-home"]; document.getElementById("head").["data-home"] = "new";
In Firefox and Google Chrome, we can call it through the getAttribute method:
document.getElementById("head").getAttribute("data-home"); document.getElementById("head").setAttribute("data-home","new");
Simple operation method in HTML5: ( The dataset attribute accesses the value of the data-* custom attribute)
This method accesses the value of the data-* custom attribute 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- attribute of all selected elements.
When using this method, instead of using the complete attribute name, such as data-home, 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 The end should be: dateOfBirth.
<p id = "head" data-home = "http://blog.csdn.net/xmtblog" data-author = "伪专家" data-date-of-birth>QQ群:135430763</p> <script type="text/javascript"> var el = document.querySelector('#head'); console.log(el.id); console.log(el.dataset);//一个DOMStringMap console.log(el.dataset.home); console.log(el.dataset.author); console.log(el.dataset.dateOfBirth); el.dataset.dateOfBirth = '1985-01-05'; // 设置data-date-of-birth的值. //判断属性 console.log('testAttr' in el.dataset);//false el.dataset.testAttr = 'testAttr'; console.log('testAttr' in el.dataset);//true </script>
If you want to delete a data-attribute, you can do this: delete el.dataset.home; or el.dataset.home = null;.
Isn’t it very convenient to operate like this? But some browsers may not support it yet. Therefore, during this period, it is best to use getAttribute and setAttribute to operate or use it with jquery.
data-attribute selector
During actual development, relevant elements can be selected based on customized data-attributes. For example, use querySelectorAll to select elements:
//Select all elements containing the 'data-p' attribute
document.querySelectorAll ('[data-p]') ;
//Select all elements containing 'data-a -href' element whose attribute value is red
document.querySelectorAll ('[data-a-href="#"]') ;
Similarly, we can also set CSS on the corresponding element through the data-attribute value Style, such as the following example:
<style type ="text/css"> .head { width : 256px ; height : 200px ; } .head[data-a='btn-a'] { color : brown } .head[data-a='btn-color'] { color : red } </style> <p class = "head" data-qq = "7" data-a = "btn-a" > button按钮 </p> <p class = "head" data-qq = "1" data-a = "btn-color" > button按钮</p>
The above is the detailed content of What are the data-* custom attributes of HTML5?. For more information, please follow other related articles on the PHP Chinese website!

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor
