在HTML5中使用数字启动ID是否有效?如果我们使用HTML5 doctype,其他技术是否支持这些id?

前端之家收集整理的这篇文章主要介绍了在HTML5中使用数字启动ID是否有效?如果我们使用HTML5 doctype,其他技术是否支持这些id?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > html id/class first symbol digit,is valid? 2个
最近有一个关于问题 here的辩论,其中答案中的用户表示ID开头的数字在HTML5中是可以接受的,那是真的吗?我没有看到关于这个主题的任何相关文档,他也在这里给我提供了 this答案的链接,但似乎相关链接已不复存在.

所以问题是,HTML5允许ID以数字开头吗?

现在可以肯定这在CSS中失败了 –

<div id="5hello">Fails Completely</div>

#5hello {
   color: red;
}

更新:我甚至不确定,HTML5允许id以数字开头,只是在元素上声明id是不够的,因为HTML本身不是一个问题,但使用<!DOCTYPE html&gt ;使用CSS和JavaScript等其他技术可以使用id开头的id.我确定CSS失败了,没有用JavaScript测试,但我相信它也会失败.

解决方法

根据这个 guy,答案是肯定的.

HTML5 gets rid of the additional restrictions on the id attribute. The
only requirements left — apart from being unique in the document — are
that the value must contain at least one character (can’t be empty),
and that it can’t contain any space characters.

您应该关注此问题的W3C文件link

3.2.3.1 The id attribute

The id attribute specifies its element’s unique identifier (ID).
[DOMCORE]

The value must be unique amongst all the IDs in the element’s home
subtree and must contain at least one character. The value must not
contain any space characters.

An element’s unique identifier can be used for a variety of purposes,
most notably as a way to link to specific parts of a document using
fragment identifiers,as a way to target an element when scripting,
and as a way to style a specific element from CSS.

Identifiers are opaque strings. Particular meanings should not be
derived from the value of the id attribute.

编辑1:我真的很喜欢这个评论,所以如果作者不介意我会把它添加到我的答案中.
“CSS失败” – 不,你只需要做正确的事:link

猜你在找的HTML5相关文章