我可以向 HTML 标记添加自定义属性吗?
我可以向 HTML 标记添加自定义属性吗?如下所示?
<tag myAttri="myVal" />
Can I add a custom attribute to an HTML tag like the following?
<tag myAttri="myVal" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
您可以随意向元素添加自定义属性。但这将使您的文件无效。
在 HTML 5 中,您将有机会使用 以
data-
为前缀的自定义数据属性。You can add custom attributes to your elements at will. But that will make your document invalid.
In HTML 5 you will have the opportunity to use custom data attributes prefixed with
data-
.您可以修改 !DOCTYPE 声明(即 DTD)以允许它,以便 [XML] 文档仍然有效:
#IMPLIED
表示它是可选属性,或者您可以使用#REQUIRED
等。更多信息请参见DTD - 属性 。
You can amend your !DOCTYPE declaration (i.e. DTD) to allow it, so that the [XML] document will still be valid:
#IMPLIED
means it is an optional attribute, or you could use#REQUIRED
, etc.More information is in DTD - Attributes.
不,这会破坏验证。
在 HTML 5 中,您可以/将能够添加自定义属性。像这样的东西:
No, this will break validation.
In HTML 5 you can/will be able to add custom attributes. Something like this:
jQuery
data()
函数允许您将任意数据与 DOM 元素关联起来。 这是一个示例。The jQuery
data()
function allows you to associate arbitrary data with DOM elements. Here's an example.在 HTML5 中:是:使用 data- 属性。
In HTML5: yes: use the data- attribute.
是的,你可以做到!
有了下一个 HTML 标签:
我们可以使用 JavaScript 访问它们的属性:
Element.setAttribute()
如果属性不存在,则将属性放入 HTML 标签中。因此,如果您要使用 JavaScript 设置它,则无需在 HTML 代码中声明它。key
:可以是您想要的属性名称,但当前标签尚未使用。value
:它始终是包含您需要的字符串。Yes, you can do it!
Having the next HTML tag:
We can access their attributes with JavaScript:
Element.setAttribute()
put the attribute in the HTML tag if not exist. So, you don't need to declare it in the HTML code if you are going to set it with JavaScript.key
: could be any name you desire for the attribute, while is not already used for the current tag.value
: it's always a string containing what you need.是的,您可以,您在问题本身中做到了:
。
Yes, you can, you did it in the question itself:
<html myAttri="myVal"/>
.您可以通过 JavaScript 设置属性。
You can set properties from JavaScript.
使用 data-any ,我经常使用它们
use data-any , I use them a lot
使用自定义元素< /a>,通常添加不带
data-
前缀的自定义属性。以下是 HTML 标准:自定义元素 中的示例(请注意
country
属性):来自 MDN Web 文档:使用自定义元素(请注意
l
和c
属性):With custom elements, it is common to add custom attributes without
data-
prefix.Here is an example from HTML standard: Custom elements (note the
country
attribute):Another example from MDN Web Docs: Using custom elements (note the
l
andc
attributes):示例如下:
这是另一个如何将自定义属性设置到 body 标签元素中的示例:
然后通过以下方式读取属性:
您可以在 DevTools 的 Console 中测试上述代码,例如
< /a>
Here is the example:
Here is another example how to set custom attributes into body tag element:
Then read the attribute by:
You can test above code in Console in DevTools, e.g.
是的,您可以使用
data-*
属性。data-*
属性用于存储页面或应用程序私有的自定义数据。Yes, you can use
data-*
attribute.The
data-*
attribute is used to store custom data private to the page or application.出色地!实际上,您可以通过将数据属性伪装成您真正想要的内容来创建一堆自定义 HTML 属性。
例如。
它显然有效,但这会使您的文档无效,除非必须,否则不需要使用 JScript 来拥有自定义属性甚至元素,您只需要像对待您的新公式化(自定义)属性一样对待您的新制定(自定义)属性“数据”属性
记住“无效”并不意味着任何事情。该文档始终可以正常加载。
有些浏览器实际上只会通过 DOCTYPE 的存在来验证您的文档......,您实际上知道我的意思。
well! you can actually create a bunch of custom HTML attributes by disguising the data attributes in what you actually want.
eg.
It apparently works but that would invalidate your document, there is no need of using JScript for you to have custom attributes or even elements unless you have to, you just need to treat your new formulated(custom) attributes just the same way you treat your "data" attribute
Remember "invalid" does not mean anything. The document will load fine at all the time.
and some browsers would actually validate your document only by the presence of DOCTYPE....., you actually know what I mean.
另一种干净且保持文档有效的方法是将您想要的数据连接到另一个标签(例如 id)中,然后使用 split 在您需要时获取您想要的数据。
Another approach, which is clean and will keep the document valid, is to concatenate the data you want into another tag e.g. id, then use split to take what you want when you want it.
我可以想到自定义标签
"init"
的一个方便用途。包含一个在document.onLoad()
时求值并为标签提供值的 JavaScript 表达式,例如,某些样板 JavaScript 代码会在
document.onload( )
查找 init 属性、评估它们包含的表达式并将它们分配给包含标签的innerHTML 的时间。这将使 HTML 具有 JSP、PHP 等的一些功能。目前,我们必须将 HTML 标记和解释它的 JavaScript 代码分开。 Bug 喜欢分割代码。I can think of a handy use for the custom tag
"init"
. Include a JavaScript expression that gets evaluated atdocument.onLoad()
time and provides a value for the tag, e.g.Some boilerplate JavaScript code would scan all the tags in the DOM at
document.onload()
time looking for the init attributes, evaluating the expressions that they contain, and assigning them to the containing tag's innerHTML. This would give HTML some of the power of JSP, PHP etc. Currently we have to split the HTML markup and the JavaScript code that illuminates it. Bugs love split code.您可以添加,但随后您还必须编写一行 JavaScript 代码,
以确保一切都到位。我的意思是 Internet Explorer :)
You can add, but then you have to write a line of JavaScript code too,
to make sure everything fall in place. I mean Internet Explorer :)
您可以执行以下操作来从 JavaScript 而不是属性中提取所需的值:
You can do something like this to extract the value you want from JavaScript instead of an attribute: