search
HomeWeb Front-endH5 TutorialComprehensive analysis of standard attributes and custom attributes in HTML5_html5 tutorial skills

As explained in HTML5 syntax, elements can contain attributes to set various properties for an element.

Some properties are defined as global and can be used on any element, while others are defined as element-specific. All properties have a name and a value, which look like the example below.

Here is an example of using HTML5 attributes, demonstrating how to mark a div element with an attribute named class and the value "example":

The

...

attribute can only be specified in the opening tag and must not be used in the closing tag.

HTML5 attributes are not case-sensitive and can be used in all uppercase or a mixture of uppercase and lowercase, although the most common convention is to always use lowercase.

Standard Attributes
The attributes listed below are supported by almost all HTML5 tags.

The "popup" title of the
属性 选项 功能
accesskey 用户自定义 定义访问元素的键盘快捷键。
align right, left, center 水平对齐标签。
background URL 在元素后面设置一个背景图像。
bgcolor 数值,十六进制值,RGB值 在元素后面设置一个背景颜色。
class 用户定义。 分类一个元素,便于使用级联样式表。
contenteditable true, false 定义用户是否可以编辑元素的内容。
contextmenu Menu id 为元素定义上下文菜单。
data-XXXX 用户定义。 自定义属性。 HTML 文档的作者可以定义自己的属性。 自定义属性必须以 "data-" 开头。
draggable true,false, auto 定义用户是否可以拖动元素。
height 数字值 定义表格,图像或表格单元的高度。
hidden hidden 定义元素是否应该可见。
id 用户定义。 命名元素,便于使用级联样式表。
item 元素列表。 用于组合元素。
itemprop 条目列表。 用于组合条目。
spellcheck true, false 定义元素是否必须有拼写或错误检查。
style CSS 样式表。 给元素定义内联样式。
subject 用户定义 id。 定义元素关联的条目。
tabindex Tab number 定于元素的 tab 键顺序。
title 用户定义。 元素的“弹出”标题。
valign top, middle, bottom HTML 元素内标签的垂直对齐方式。
width 数字值。 定义表格,图像和表格单元的宽度。
Attributes
Options Function
accesskey User-defined Define keyboard shortcuts for accessing elements.
align right, left, center Align labels horizontally.
background URL Set a background image behind the element.
bgcolor Numerical value, hexadecimal value, RGB value Sets a background color behind the element.
class User defined. Categories an element to facilitate the use of cascading style sheets.
contenteditable true, false Defines whether users can edit the content of the element.
contextmenu Menu id Define a context menu for the element.
data-XXXX User defined. Custom attributes. Authors of HTML documents can define their own attributes. Custom properties must start with "data-".
draggable true,false,auto Defines whether the user can drag the element.
height numeric value Define the height of a table, image, or table cell.
hidden hidden Defines whether the element should be visible.
id User defined. named elements to facilitate the use of cascading style sheets.
item element list. is used to combine elements.
itemprop List of entries. is used to combine entries.
spellcheck true, false Defines whether the element must have spelling or error checking.
style CSS style sheet. defines inline styles for elements.
subject User-defined id. defines the items associated with the element.
tabindex Tab number is determined by the tab order of the elements.
title User defined. element.
valign top, middle, bottom Vertical alignment of tags within HTML elements.
width Numeric value. Defines the width of tables, images and table cells.

Custom attributes
HTML5 also introduces a new feature, which is the ability to add custom data attributes.

Custom data attributes start with data- and are named based on our needs. Here is a simple example:


...

Two of the above examples Custom attributes called data-subject and data-level are fully valid in HTML5. We can also get their values ​​using the JavaScript API or in CSS in a similar way to standard properties.

Add custom attributes to HTML elements and access them through JavaScript. If you have tried it before, you will find that it is easy to ignore tag validation, and HTML5 can provide you with the ability to create and use your own elements within a valid web page. Attribute function.

Create HTML5 file:

If you haven’t decided which one to use yet, you can copy the code below:

XML/HTML CodeCopy content to clipboard
  1. /span>
  2. >
  3. html
  4. >
  5. head
  6. >
  7. script
  8. >
  9. /*functions here*/ 
  10. script
  11. >
  12. head
  13. >
  14. body
  15. >
  16. body
  17. >
  18. html
  19. >


Set custom elements in the body and access them using JavaScript elements in the head script area.

Create element:

First, add some simple content and elements such as custom attributes and IDs so that we can identify the JavaScript example.

XML/HTML CodeCopy content to clipboard
  1. div id="product1" data-product-category="clothing"
  2. >
  3. Cotton Shirt
  4. div
  5. >


As you can see, the custom attribute has the form: "data-*", set the name in the "data-" part or a name of your choice. Use custom attributes in HTML5, this is the only way that works. Therefore, only use this method if you want to verify whether the web page is valid.

Of course, the project details determine whether the custom property is useful to you and how you name it. This example can be applied to retail websites across different product categories.

Custom attributes allow you to use JavaScript code within the page to set elements in a special way, for example, to animate the display function. If there are no standard HTML elements, we recommend using custom attributes.

Add test button

Use your own JavaScript elements on the page to execute events, provided that the following code is added to the page:

XML/HTML CodeCopy content to clipboard
  1. input type="button" value="get attribute" onclick="getElementAttribute ('product1')"
  2. />


Get attributes:

The most common way to access attributes in JavaScript is to use "getAttributes", which is also the first step we need to do. Add the following function in the head script area of ​​the page:

JavaScript CodeCopy content to clipboard
  1. function getElementAttribute(elemID) {
  2. var theElement = document.getElementById(elemID);
  3. var theAttribute = theElement.getAttribute('data-product-category'); 🎜>
  4. alert(theAttribute);

Here, we have added the alert value to the example. Of course, you can also add it in the script according to your own needs.

Get data:

You can use an element dataset instead of DOM "getAttributes", which may be more efficient, especially in certain cases where the code iterates through multiple attributes. However, browser support for datasets is still very low. So keeping that in mind, this code executes the same process as the method // after.

//var theAttribute = theElement.getAttribute('data-product-category');
var theAttribute = theElement.dataset.productCategory;
Remove "data-" from the attribute name in the dataset, it Still included in the HTML.

Please note that if your custom attribute name has a hyphen in it, this will appear in camel-case form when accessed via data, i.e. ("data-product-category" becomes "productCategory").

Other modules and functions

We have obtained the property, scripts can still set and remove it. The code below demonstrates how to set properties using standard JavaScript modules and datasets.

JavaScript CodeCopy content to clipboard
  1. //DOM method
  2. theElement.setAttribute('data-product-category', 'sale');
  3. //dataset version
  4. theElement.dataset.productCategory = "sale";
  5. You can also use DOM methods or data sets to delete an attribute:
  6. //DOM method
  7. theElement.removeAttribute('data-product-category');
  8. //dataset version
  9. theElement.dataset.productCategory = null;
Implementing custom attributes in HTML5 is not technically very complicated. The real difficulty is whether the method you choose to use is suitable for your project; if so, how to make it more effective? Please keep in mind that it is still too early to enable the dataset method as a page feature, as many browsers do not support this feature yet.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 Code: Accessibility and Semantic HTMLH5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AM

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

Is h5 same as HTML5?Is h5 same as HTML5?Apr 08, 2025 am 12:16 AM

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

What is the function of H5?What is the function of H5?Apr 07, 2025 am 12:10 AM

H5, or HTML5, is the fifth version of HTML. It provides developers with a stronger tool set, making it easier to create complex web applications. The core functions of H5 include: 1) elements that allow drawing graphics and animations on web pages; 2) semantic tags such as, etc. to make the web page structure clear and conducive to SEO optimization; 3) new APIs such as GeolocationAPI support location-based services; 4) Cross-browser compatibility needs to be ensured through compatibility testing and Polyfill library.

How to do h5 linkHow to do h5 linkApr 06, 2025 pm 12:39 PM

How to create an H5 link? Determine the link target: Get the URL of the H5 page or application. Create HTML anchors: Use the <a> tag to create an anchor and specify the link target URL. Set link properties (optional): Set target, title, and onclick properties as needed. Add to webpage: Add HTML anchor code to the webpage where you want the link to appear.

How to solve the h5 compatibility problemHow to solve the h5 compatibility problemApr 06, 2025 pm 12:36 PM

Solutions to H5 compatibility issues include: using responsive design that allows web pages to adjust layouts according to screen size. Use cross-browser testing tools to test compatibility before release. Use Polyfill to provide support for new APIs for older browsers. Follow web standards and use effective code and best practices. Use CSS preprocessors to simplify CSS code and improve readability. Optimize images, reduce web page size and speed up loading. Enable HTTPS to ensure the security of the website.

How to generate links with h5How to generate links with h5Apr 06, 2025 pm 12:33 PM

h5 pages can generate links in two ways: create links manually or use short link services. By manually creating, you just need to copy the URL of the h5 page; through the short link service, you need to paste the URL into the service and then get the shortened URL.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

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.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.