我可以向 HTML 标记添加自定义属性吗?

2022-01-30 00:00:00 html custom-attribute

我可以像下面这样向 HTML 标记添加自定义属性吗?

Can I add a custom attribute to an HTML tag like the following?

<tag myAttri="myVal" />

推荐答案

您可以修改您的 !DOCTYPE 声明(即 DTD)以允许它,以便 [XML] 文档仍然有效:

You can amend your !DOCTYPE declaration (i.e. DTD) to allow it, so that the [XML] document will still be valid:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
[
  <!ATTLIST tag myAttri CDATA #IMPLIED>
]>

#IMPLIED 表示它是可选属性,也可以使用#REQUIRED

#IMPLIED means it is an optional attribute, or you could use #REQUIRED, etc.

DTD - 属性中有更多信息.

More information is in DTD - Attributes.

相关文章