Can an id attributes value start with a numb开发者_开发问答er?
For HTML, no - http://www.w3.org/TR/html401/types.html#type-name
Theoretically, the collection of legal values in XHTML could be much larger however I doubt it would be compatible with many browsers. See http://www.w3.org/TR/xhtml1/#C_8
The currently accepted answer is actually incorrect.
All browsers have always supported ID attributes that start with a number, and HTML5 now allows it. See http://mathiasbynens.be/notes/html5-id-class:
The HTML 4.01 spec states that
ID
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 (.
). For theclass
attribute, there is no such limitation. Classnames can contain any character, and they don’t have to start with a letter to be valid.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.
Note that you will need to escape the ID attribute value correctly if you want to use it in CSS or JavaScript. See http://mothereffingcssescapes.com/#123-foo.
精彩评论