什么是data- * HTML属性? [英] what are data-* HTML attributes?

查看:106
本文介绍了什么是data- * HTML属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在一个网站的开头标签中发现了这样的内容:

I've recently found on one of the sites opening tag like this:

<script data-ip="93.1xx.3.2x" data-backuri="something.com">

我找不到有关它的任何信息.这些标签是用来干什么的?

And I couldn't find any information about it. What are those tags used for?

推荐答案

data-*属性是自定义HTML属性.

data-* attributes are custom HTML attributes.

基本上,有标准的HTML属性,例如style, src, width, height, class ...,这些属性对浏览器具有特殊的含义,并且是保留的".

Basically, there is standard HTML attributes like style, src, width, height, class... and these have a special meaning to browsers and are 'reserved'.

但是,自定义属性通常没有特殊含义,仅对所有者应用程序特殊.它们可用于简化应用程序逻辑.

However, custom attributes have no special meaning generally and are only special to the owners application. They can be used to simplify an applications logic.

在属性名称之前使用data-可确保将来的标准属性不会使用您的当前属性.例如,假设今天您正在使用sound属性.如果您使用过data-sound,您会很好的,因为规范指出将来的浏览器将不会使用它.而是,将来的标准浏览器属性之前都不会包含data-.

Using data- before your attribute name ensures that future standard attributes will not use your current attribute. For example, imagine today you are using a sound attribute. Had you used data-sound, you would be fine because specification says it will not be used by future browsers. Rather, no future standard browser attributes will contain data- before them.

请参见 jquery获取HTML 5数据属性连字符和区分大小写,以获取有关为什么使用data- *属性的一些有用信息.

See jquery get HTML 5 Data Attributes with hyphens and Case Sensitivity for some useful info on why we use data-* attributes.

另外,请参见 MDN文档有用的信息.

Also, see MDN docs for some useful information.

这篇关于什么是data- * HTML属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆