为什么我不能将数值作为元素的ID? [英] Why can't I have a numeric value as the ID of an element?

查看:175
本文介绍了为什么我不能将数值作为元素的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



< blockquote>

< >属性id具有无效值567属性ID是类型ID。如上所述,它应该以字母开头并且没有空格




 好< a id =567href =/ index.html> 
好​​< a id =n567href =/ index.html>

我可以通过我的代码并添加一个字母,然后在我的jQuery中使用该值时将其去掉但它会搅乱我真的不需要。

是否有我不应该使用数字作为ID的原因?

解决方案

这就是规范所说的。 HTML 4规范


ID NAME 令牌必须以字母([A-Za-z])开头,后面可以跟随任意数量的字母,数字([0

),连字符( - ),
下划线(_),冒号(:)和句点(。

好消息是 HTML 5规范更宽松:
$ b


id 属性指定其元素的唯一标识符(ID)
该元素的主
子树中的所有ID必须是唯一的,并且必须至少包含一个字符。值不能
包含任何空格字符。



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?

解决方案

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.

这篇关于为什么我不能将数值作为元素的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆