问题描述
在我的项目中,我有提交和评论,每个都有一个ID。当前的ID只是数字,对应于他们的数据库ID。一切正常工作,但当我通过W3验证程序运行它得到的错误:
In my project I have submissions and comments, each with an ID. Currently the ID's are just numeric and correspond to their database ID's. Everything is working fine but when I run it through the W3 validator I get the error:
属性id的值无效:1不能开始一个名字
我想,我可以在所有ids之前的某种字符串,但然后每当我使用或操纵id在JQuery或PHP我必须做一个id.replace('string','')在使用它之前。这似乎很麻烦。任何建议?
I suppose instead that I could just precede all ids with some sort of string but then whenever I was using or manipulating the id in JQuery or PHP I have to do a id.replace('string', '') before using it. This seems rather cumbersome. Any advice?
推荐答案
是的,使用数字作为HTML元素ID是不好的做法。
Yes, using numbers as HTML element IDs is bad practice.
-
违反W3C规范。
您在问题中注意到了这一点, HTML5
It violates W3C specification.
You noted this in your question, and it is true for every HTML specification except HTML5
它不利于SEO。
搜索引擎优化的HTML元素ID应反映内容的标识元素。签出 ,由Meitar Moscovitz编写。
It is detrimental to SEO.
Search-engine optimized HTML element IDs SHOULD reflect the content of the identified element. Check out How To Compose HTML ID and Class Names like a Rockstar by Meitar Moscovitz. It provides a good overview of this concept.
可能存在服务器端脚本问题。
回到我第一次开始在ASP经典编程时,我不得不通过 Request.Form(some_id)
的语法访问提交的表单字段。但是,如果我做了 Request.Form(1)
,它将返回表单集合中第二个字段的值,而不是Id等于1的元素。是处理集合的一个非常标准的行为。它也与javascript类似,并可能使您的客户端脚本更复杂,以维持。
There can be server-side scripting issues.
Back when I first started programming in ASP classic, I had to access submitted form fields by a syntax like Request.Form("some_id")
. However, if I did Request.Form(1)
it would return the value of the second field in the form collection, instead of the element with an Id equal to 1. This is a pretty standard behavior for working with collections. Its also similar with javascript, and could make your client side scripting more complicated to maintain as well.
这篇关于用数字启动ID总是不好的做法吗? (CSS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!