ID属性能以冒号开头吗? [英] Can an ID attribute start with colon?

查看:181
本文介绍了ID属性能以冒号开头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < input id =: 3f4
name =attach
type =checkbox
value =13777be311c96bab_13777be311c96bab_0.1_-1
checked =>



奇迹奇观,大多数元素的ID都以开头:

我一直认为ID属性的定义是这样的。


ID和NAME标记必须以字母([A-Za-z])开始,可以是
后跟任意数量的字母,数字([0-9]),连字符( - ),
下划线(或者我错过了什么新的东西?),冒号(:)和句点(。)。

我的意思是用HTML5可以吗?

允许在最新的工作草案中: http:// www.w3.org/TR/html-markup/datatypes.html#common.data.id


任何字符串,具有以下限制:

- 必须至少包含一个字符

- 不能包含任何空格字符

该规范还指出:


以前版本的HTML对内容
设置了更大的限制值(例如,他们不允许ID值以
a数字开头)。

您引用的定义出现在 HTML 4规范

有一个广泛访问的SO线程,它访问了一些有关ID的注意事项(主要来自HTML 4 b

理由



在仔细想想之后,我意识到这里有两个很好的问题:

为什么规范允许这样?



角色有可能破坏各种各样的东西,比如CSS选择器(如果不使用适当的转义),Sizzle(jQuery使用的)模式匹配,服务器ID(例如ASP.Net web表单使用)和生成的ID从模型属性(比如可以用MVC模式)。



除了所有这些,我认为HTML 5的一个关键目标是 not 创建并非绝对必要的限制(这是XHTML的一个缺点)。仅仅因为某个目的尚未被识别出来并不意味着它不会在未来。



尽管赢了很多东西't 工作,某些事情可以正常工作,例如 document.getElementById(:foo)



http://jsfiddle.net/Xjast/



与大多数情况一样,开发人员需要了解他或她正在使用的工具。



为什么Google这样做吗?



显然,除非您是Gmail团队的成员,否则无法完全回答。但是,Google大量减少并混淆了他们的代码;他们还管理着大量的脚本,这表明了明确的约定。



这是另一个想法。如果Google正在利用CSS选择器需要转义特定字符的事实呢?这将大大减少电子邮件中包含的内容的意外重新设置。


Was viewing the source of Gmail for purely academic purposes and I came across this.

<input id=":3f4" 
    name="attach" 
    type="checkbox" 
    value="13777be311c96bab_13777be311c96bab_0.1_-1" 
    checked="">

Wonder of wonders, most elements have ids that starts with a :
I always thought the definition for ID attribute was this.

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 (".").

Or am I missing anything new? I mean is that OK with HTML5?

解决方案

Permissibility

It is allowed in the latest working draft: http://www.w3.org/TR/html-markup/datatypes.html#common.data.id

Any string, with the following restrictions:
- must be at least one character long
- must not contain any space characters

The spec also notes:

Previous versions of HTML placed greater restrictions on the content of ID values (for example, they did not permit ID values to begin with a number).

The definition you quoted appears in the HTML 4 spec.

There is a widely-visited SO thread which visits some of considerations regarding IDs (mainly from an HTML 4 perspective).

Rationale

After thinking about it more, I realized that there are two good questions here:

Why does the spec allow this?

IDs which can contain any character have the potential to break all sorts of things, such as CSS selectors (if proper escaping is not used), Sizzle (which jQuery uses) pattern matches, server IDs (such as ASP.Net web forms use) and IDs which are generated from model properties (such as one might do with a MVC pattern).

All those things aside, I believe a key goal of HTML 5 was to not create restrictions that weren't absolutely necessary (which was a shortcoming of XHTML). Just because a purpose hasn't been identified for something yet doesn't mean that it won't be in the future.

Despite the many things which won't work, certain things work just fine, for example document.getElementById(":foo")

http://jsfiddle.net/Xjast/

As with most things, it is up to the developer to be knowledgeable of the tools that he or she is using.

Why does Google do this?

Obviously this can't be answered conclusively unless you are part of the Gmail team. However, Google heavily minimizes and obfuscates their code; they also manage a huge amount of script, which suggests well-defined conventions.

Here's another thought. What if Google is leveraging the fact that CSS selectors require escaping of certain characters? This would go a long way towards reducing accidental restyling of content contained in an email message.

这篇关于ID属性能以冒号开头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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