javascript HTML 元素 id 的合法字符是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4247840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:46:36  来源:igfitidea点击:

What are legal characters for an HTML element id?

javascripthtmlcss

提问by Tony the Pony

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

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

For example:

例如:

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

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

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

(或者更确切地说,我应该坚持使用某些字符以确保id在所有主要浏览器/JavaScript 引擎上都能正常工作)。

采纳答案by Tim Down

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

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

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

正如其他答案所指出的那样,HTML 4 的限制性更强,并指定

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

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

回答by wajiw

In HTML 4, the idattribute holds a NAME tokenwhich are defined:

在HTML 4中,id属性持有NAME令牌定义

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

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

As pointed out in a previous post, depending on your implementation you may have troubles with colons and periods along with others: What are valid values for the id attribute in HTML?

正如在前一篇文章中所指出的,根据您的实现,您可能会遇到冒号和句点以及其他问题:HTML 中 id 属性的有效值是什么?

回答by AndreKR

From http://www.w3.org/TR/html4/types.html:

http://www.w3.org/TR/html4/types.html

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

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

回答by ajreal

All the above answers + IDmust be unique

以上所有答案+ ID必须是唯一的

回答by asthasr

From the HTML4 specification:

HTML4 规范

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

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

回答by Gabriele Pala

actually I can successfully use a ° too for example

实际上我也可以成功地使用°例如

< span id= "test°" />

< span id="test°" />

Seems to be allowed, and I have no problem with jQuery or other ways to fetch dom elements via the selectors. Perhaps other chars work, but I didn't test them all.

似乎是允许的,我对 jQuery 或其他通过选择器获取 dom 元素的方法没有任何问题。也许其他字符有效,但我没有全部测试。