HTML元素ID的合法字符是什么? [英] What are legal characters for an HTML element id?

查看:879
本文介绍了HTML元素ID的合法字符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在HTML元素的标识符中使用哪些字符?

What characters can I use in an identifier for an HTML element?

例如:

< SPAN id =section [5] ...>

(或者我应该坚持使用某些字符来确保 id 适用于所有主流浏览器/ JavaScript引擎)。

(Or rather, should I stick to certain characters to make sure the id works across all major browsers/JavaScript engine).

推荐答案

在HTML5中,唯一的限制是ID在文档中必须是唯一的,至少包含一个字符,空间。请参见 http://www.w3.org/TR/2014 /REC-html5-20141028/dom.html#the-id-attribute

In HTML5, the only restrictions are that the ID must be unique within the document, contain at least one character and contain no spaces. See http://www.w3.org/TR/2014/REC-html5-20141028/dom.html#the-id-attribute

如其他答案所指出的,HTML 4更具限制性,并指定

As other answers have pointed out, HTML 4 is more restrictive and specifies that


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

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

这篇关于HTML元素ID的合法字符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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