DOM标识中不允许的字符(按规范)和浏览器 [英] characters not allowed in DOM ids by spec, and by browser

查看:161
本文介绍了DOM标识中不允许的字符(按规范)和浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题是关于id属性的内容的详细描述:
在DOM ID中允许使用什么字符?



我的问题是,浏览器如何遵守这个规范?我非常确定我可以使用所有的数字ids在firefox例如。

解决方案

这是HTML4和HTML5的区别。 / p>

由于此 answer 说,HTML4中的id在允许的范围内是非常严格的。



HTML4中的有效ID符合 spec


必须以字母([A-Za-z])开头,后面可以跟随任意数量的字母,数字([0-9 ]),连字符( - ),下划线(_)冒号(:)和句点(。)


HTML5是一个更轻松的id。根据 HTML5规范的有效ID, / p>


在元素的home子树的所有ID中必须是唯一的,且必须至少包含一个字符。


由于浏览器正在转换为HTML5(大多数已经实现了这些基本功能),您可能会发现大多数现代浏览器都将遵循HTML5定义ids的方式。仍然最好的方法是测试它或有支持文档,以确切知道哪些浏览器是和不工作。


The following question is a good rundown on what the spec says about the contents of the id attribute: What characters are allowed in DOM IDs?

my question is, how well do browsers adhere to this spec? I am pretty sure that i can use all numeric ids in firefox for example.

解决方案

That's the difference in HTML4 vs HTML5.

As this answer says, id's in HTML4 were highly restrictive on what is allowed.

A valid id in HTML4 as per the spec,

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

HTML5 is a lot more relaxed with id's. A valid id as per the HTML5 spec,

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.

Since browsers are transitioning to HTML5 (most have already implemented these basics), you might find that most modern browsers will adhere to the HTML5 way of defining ids. Still the best way is to test it out or have backing documentation to know exactly which browsers do and don't work.

这篇关于DOM标识中不允许的字符(按规范)和浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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