我可以在我的HTML标签上设置属性吗? [英] Can I just make up attributes on my HTML tags?
问题描述
我是否允许将任何我想要的属性添加到HTML标记中,以便稍后可以使用JavaScript检索它们的值?例如:
Am I allowed to add whatever attributes I want to HTML tags such that I can retrieve their value later on using javascript? For example:
<a href="something.html" hastooltip="yes" tipcolour="yellow">...</a>
如果这不起作用,您将如何存储任意类似的信息?
If that's not going to work, how would you store arbitrary pieces of information like this?
编辑:由于看起来组成HTML属性在技术上并不合理,因此我在此将这个问题的第二部分转化为自己的问题:如何为某些HTML标记存储任意数据
Since it appears that making up HTML attributes isn't technically valid, I've rephrased the second part of this question into its own question here: How to store arbitrary data for some HTML tags
推荐答案
在HTML5中,是的。你只需要用 data -
来加前缀。请参阅规范。
In HTML5, yes. You just have to prefix them with data-
. See the spec.
当然,这意味着即使浏览器不在意,也应该使用HTML5文档类型(<!doctype html>
)。
Of course, this implies you should be using the HTML5 doctype (<!doctype html>
), even though browsers don't care.
这篇关于我可以在我的HTML标签上设置属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!