DOM中的字符不允许通过spec和浏览器 [英] characters not allowed in DOM ids by spec, and by browser

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

问题描述

以下问题是关于id属性内容的详细说明:
DOM ID中允许哪些字符?



我的问题是,浏览器是否遵守此规范?我很确定我可以使用firefox中的所有数字ids。

解决方案

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

由于这个答案说,HTML4中的id对于允许的是高度限制。



根据 spec


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


HTML5是一个更加轻松的id。根据 HTML5规范


在元素的家庭子树中的所有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中的字符不允许通过spec和浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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