Why can't I have a numeric value as the ID of an element?

▼魔方 西西 提交于 2019-11-26 03:20:54

问题


Working on a project, nearly finished and just tidying up the HTML and I find out that you\'re not really allowed to have an ID that is just a number-

< 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\">

I can go through my code and add a letter then strip it when the value is used in my jQuery but it would be messing around I don\'t really need.

Is there a reason I shouldn\'t be using numbers as ID\'s?


回答1:


That's just what the spec says.

From the 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 (".").

The good news is that the HTML 5 specification is more lenient:

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.




回答2:


Why can't I have a numeric value as the ID of an element?

Because that's what the HTML4 spec dictates.


On the other hand, the HTML5 spec has removed this requirement.



来源:https://stackoverflow.com/questions/7987636/why-cant-i-have-a-numeric-value-as-the-id-of-an-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!