html – 为什么我不能将数值作为元素的ID?

一个项目上工作,几乎完成了,只是整理了HTML,我发现你不是真的被允许有一个只是一个数字的ID,

< a> attribute “id” has invalid value “567” The attribute ID is of type ID. As described above,it should begin with a letter and have no spaces

Good    <a id="567" href="/index.html">
Good    <a id="n567" href="/index.html">

我可以通过我的代码,并添加一个字母,然后剥离它,当该值用于我的jQuery,但它会搞乱我真的不需要.

有没有理由我不应该使用数字作为ID?

解决方法

这就是规范说的.

HTML 4 specification

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters,digits ([0-9]),hyphens (“-“),
underscores (“_”),colons (“:”),and periods (“.”).

好消息是,HTML 5 specification更宽松:

The id attribute specifies its element’s unique identifier (ID). 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.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些