The following question is a 开发者_如何学Cgood rundown on what the spec says about the contents of the id attribute: What characters are allowed in DOM IDs?
my question is, how well do browsers adhere to this spec? I am pretty sure that i can use all numeric ids in firefox for example.
That's the difference in HTML4 vs HTML5.
As this answer says, id's in HTML4 were highly restrictive on what is allowed.
A valid id in HTML4 as per the spec,
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(".")
.
HTML5 is a lot more relaxed with id's. A valid id as per the HTML5 spec,
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.
Since browsers are transitioning to HTML5 (most have already implemented these basics), you might find that most modern browsers will adhere to the HTML5 way of defining ids. Still the best way is to test it out or have backing documentation to know exactly which browsers do and don't work.
What is the purpose of this question? You clearly realize it is not a good idea. Many browsers allow invalid code to be written because they don't want to serve damaged pages to users.
You shouldn't do it, because there's a strong chance it will break yesterday, today, and certainly tomorrow. So there's no need to ask about it.
精彩评论