gpt4 book ai didi

html - 为什么我不能用数值作为元素的 ID?

转载 作者:技术小花猫 更新时间:2023-10-29 12:09:33 24 4
gpt4 key购买 nike

正在做一个项目,快要完成了,只是整理 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.

关于html - 为什么我不能用数值作为元素的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7987636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com