简体   繁体   English

使用数字启动ID始终是不好的做法吗? (CSS)

[英]Is it always bad practice to start an ID with a number? (CSS)

In my project I have submissions and comments, each with an ID. 在我的项目中,我有提交和评论,每个都有一个ID。 Currently the ID's are just numeric and correspond to their database ID's. 目前,ID只是数字,与其数据库ID相对应。 Everything is working fine but when I run it through the W3 validator I get the error: 一切正常,但是当我通过W3验证器运行时,我收到错误:

value of attribute "id" invalid: "1" cannot start a name

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. 我想相反,我可以在所有id之前添加某种字符串,但每当我在JQuery或PHP中使用或操作id时,我必须在使用之前执行id.replace('string','')。 This seems rather cumbersome. 这看起来相当麻烦。 Any advice? 有什么建议?

Yes, using numbers as HTML element IDs is bad practice. 是的,使用数字作为HTML元素ID是不好的做法。

  1. It violates W3C specification. 它违反了W3C规范。
    You noted this in your question, and it is true for every HTML specification except HTML5 您在问题中注意到了这一点,除了HTML5之外,每个HTML规范都是如此

  2. It is detrimental to SEO. 这对SEO是有害的。
    Search-engine optimized HTML element IDs SHOULD reflect the content of the identified element. 搜索引擎优化的HTML元素ID应该反映所标识元素的内容。 Check out How To Compose HTML ID and Class Names like a Rockstar by Meitar Moscovitz. 查看Meitar Moscovitz 如何编写像Rockstar一样的HTML ID和类名 It provides a good overview of this concept. 它提供了这个概念的良好概述。

  3. 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") . 回到我第一次开始使用ASP经典编程时,我不得不通过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. 但是,如果我执行Request.Form(1) ,它将返回表单集合中第二个字段的值,而不是Id等于1的元素。这是使用集合的非常标准的行为。 Its also similar with javascript, and could make your client side scripting more complicated to maintain as well. 它也与javascript类似,并且可能使您的客户端脚本更难以维护。

I suggest you to use prefixes "comment-ID" or "post-ID". 我建议你使用前缀“comment-ID”或“post-ID”。

If you need the id in JavaScript, you just have to id.substring(8) (for "comment-") 如果你需要JavaScript中的id,你只需要id.substring(8) (用于“comment-”)

The HTML 5 Specification lifts this restriction. HTML 5规范解除了这一限制。 If you're worried about validity you might simply consider changing the DTD to HTML5's. 如果您担心有效性,可以考虑将DTD更改为HTML5。

http://www.w3.org/TR/html5/elements.html#the-id-attribute http://www.w3.org/TR/html5/elements.html#the-id-attribute

如果您正在操作元素,那么您可以使用$(this).jQueryOperation() - 因此您可以拥有前缀而无需替换任何内容!

The best way for your need is having the prefix for your class, I mean something like item- x and x is the number that you need. 你需要的最好的方法是为你的班级提供前缀,我的意思是像item- xx是你需要的数字。

But from my personal experience, it is better to use classes for your elements, and you know that you must use classes if the item is not unique in the page 但根据我的个人经验,最好为您的元素使用类,并且您知道如果该项在页面中不唯一,则必须使用类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM