DOM id中允许哪些字符?(复制)

时间:2021-10-29 22:32:12

Possible Duplicate:
What are valid values for the id attribute in HTML?

可能重复:在HTML中id属性的有效值是什么?

Underscores seem fine. What about dashes? Other special characters?

强调了看起来很好。破折号是什么呢?其他特殊字符?

5 个解决方案

#1


99  

Actually there is a difference between HTML and XHTML. As XHTML is XML the rules for XML IDs apply:

实际上,HTML和XHTML之间是有区别的。由于XHTML是XML,所以适用XML id规则:

Values of type ID MUST match the Name production.

类型ID的值必须与名称生成匹配。

NameStartChar ::=   ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] |
                          [#xD8-#xF6] | [#xF8-#x2FF] |
                          [#x370-#x37D] | [#x37F-#x1FFF] |
                          [#x200C-#x200D] | [#x2070-#x218F] |
                          [#x2C00-#x2FEF] | [#x3001-#xD7FF] |
                          [#xF900-#xFDCF] | [#xFDF0-#xFFFD] |
                          [#x10000-#xEFFFF]

NameChar     ::=      NameStartChar | "-" | "." | [0-9] | #xB7 |
                        [#x0300-#x036F] | [#x203F-#x2040]

Source: Extensible Markup Language (XML) 1.0 (Fifth Edition) 2.3

来源:可扩展标记语言(XML) 1.0(第五版)2.3

For HTML the following applies:

对于HTML,适用以下内容:

id = name [CS]
This attribute assigns a name to an element. This name must be unique in a document.

id = name [CS]此属性为元素分配一个名称。这个名称在文档中必须是唯一的。

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])、连字符("-")、下划线("_")、冒号(":")和句点(".")。

Source: HTML 4 Specification, Chapter 6, ID Token

源:HTML 4规范,第6章,ID令牌

#2


21  

The W3C spec Basic HTML data types says "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 (".")."

W3C规范基本的HTML数据类型表示“ID和NAME标记必须以字母开头([a- za -z]),然后可能会有任意数量的字母、数字([0-9])、连字符("-")、下划线("_")、冒号(":")和句点(".")。

#3


7  

If we take the title of your question literally, then neither the HTML nor XHTML rules apply. Instead, the relevant spec is the DOM one.

如果我们从字面上理解问题的标题,那么HTML和XHTML规则都不适用。相反,相关的规范是DOM规范。

Taking DOM Level 3 as our source, and assuming that by "DOM ID" you mean an attribute with the "ID" flag set, then the value is a "DOMString", the characters of which can be any UTF-16 encodable character.

以DOM Level 3为源,并假设通过“DOM ID”,您是指带有“ID”标志集的属性,那么该值就是一个“DOMString”,其中的字符可以是任何UTF-16可编码字符。

16-bit unit

16位单位

The base unit of a DOMString. This indicates that indexing on a DOMString occurs in units of 16 bits. This must not be misunderstood to mean that a DOMString can store arbitrary 16-bit units. A DOMString is a character string encoded in UTF-16; this means that the restrictions of UTF-16 as well as the other relevant restrictions on character strings must be maintained. A single character, for example in the form of a numeric character reference, may correspond to one or two 16-bit units.

DOMString的基元。这表明在一个DOMString上的索引发生在16位的单位中。不能误解为DOMString可以存储任意16位的单元。DOMString是用UTF-16编码的字符串;这意味着必须维护UTF-16的限制以及其他有关字符串的限制。一个字符,例如以数字字符引用的形式,可以对应一个或两个16位单元。

Of course, this is probably not what you want, and that Ludwig Weinzierl's answer is what you were looking for. However it is wise to understand that not all DOMs can be serialized as HTML or XHTML and that the DOM has it's own set of rules.

当然,这可能不是你想要的,路德维希·温泽尔的回答是你想要的。然而,明智的是要理解并不是所有的DOM都可以序列化为HTML或XHTML, DOM有它自己的一套规则。

#4


4  

According to the HTML 4.0 specs

根据HTML 4.0规范

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])、连字符("-")、下划线("_")、冒号(":")和句点(".")。

#5


2  

For purposes of valid html aka xhtml, Philippe is correct. No spaces or special characters (certainly none that require escaping) for id attributes. Just the 52 character alphabet, upper and lower case, numerals 0-9, hyphens ("-"), underscores ("_"), colons (":"), and periods (".")

对于有效的html即xhtml, Philippe是正确的。id属性没有空格或特殊字符(当然没有需要转义的字符)。只有52个字符字母表,大写和小写,数字0-9,连字符(“-”),下划线(“_”),冒号(“:”)和句点(“。”)

#1


99  

Actually there is a difference between HTML and XHTML. As XHTML is XML the rules for XML IDs apply:

实际上,HTML和XHTML之间是有区别的。由于XHTML是XML,所以适用XML id规则:

Values of type ID MUST match the Name production.

类型ID的值必须与名称生成匹配。

NameStartChar ::=   ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] |
                          [#xD8-#xF6] | [#xF8-#x2FF] |
                          [#x370-#x37D] | [#x37F-#x1FFF] |
                          [#x200C-#x200D] | [#x2070-#x218F] |
                          [#x2C00-#x2FEF] | [#x3001-#xD7FF] |
                          [#xF900-#xFDCF] | [#xFDF0-#xFFFD] |
                          [#x10000-#xEFFFF]

NameChar     ::=      NameStartChar | "-" | "." | [0-9] | #xB7 |
                        [#x0300-#x036F] | [#x203F-#x2040]

Source: Extensible Markup Language (XML) 1.0 (Fifth Edition) 2.3

来源:可扩展标记语言(XML) 1.0(第五版)2.3

For HTML the following applies:

对于HTML,适用以下内容:

id = name [CS]
This attribute assigns a name to an element. This name must be unique in a document.

id = name [CS]此属性为元素分配一个名称。这个名称在文档中必须是唯一的。

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])、连字符("-")、下划线("_")、冒号(":")和句点(".")。

Source: HTML 4 Specification, Chapter 6, ID Token

源:HTML 4规范,第6章,ID令牌

#2


21  

The W3C spec Basic HTML data types says "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 (".")."

W3C规范基本的HTML数据类型表示“ID和NAME标记必须以字母开头([a- za -z]),然后可能会有任意数量的字母、数字([0-9])、连字符("-")、下划线("_")、冒号(":")和句点(".")。

#3


7  

If we take the title of your question literally, then neither the HTML nor XHTML rules apply. Instead, the relevant spec is the DOM one.

如果我们从字面上理解问题的标题,那么HTML和XHTML规则都不适用。相反,相关的规范是DOM规范。

Taking DOM Level 3 as our source, and assuming that by "DOM ID" you mean an attribute with the "ID" flag set, then the value is a "DOMString", the characters of which can be any UTF-16 encodable character.

以DOM Level 3为源,并假设通过“DOM ID”,您是指带有“ID”标志集的属性,那么该值就是一个“DOMString”,其中的字符可以是任何UTF-16可编码字符。

16-bit unit

16位单位

The base unit of a DOMString. This indicates that indexing on a DOMString occurs in units of 16 bits. This must not be misunderstood to mean that a DOMString can store arbitrary 16-bit units. A DOMString is a character string encoded in UTF-16; this means that the restrictions of UTF-16 as well as the other relevant restrictions on character strings must be maintained. A single character, for example in the form of a numeric character reference, may correspond to one or two 16-bit units.

DOMString的基元。这表明在一个DOMString上的索引发生在16位的单位中。不能误解为DOMString可以存储任意16位的单元。DOMString是用UTF-16编码的字符串;这意味着必须维护UTF-16的限制以及其他有关字符串的限制。一个字符,例如以数字字符引用的形式,可以对应一个或两个16位单元。

Of course, this is probably not what you want, and that Ludwig Weinzierl's answer is what you were looking for. However it is wise to understand that not all DOMs can be serialized as HTML or XHTML and that the DOM has it's own set of rules.

当然,这可能不是你想要的,路德维希·温泽尔的回答是你想要的。然而,明智的是要理解并不是所有的DOM都可以序列化为HTML或XHTML, DOM有它自己的一套规则。

#4


4  

According to the HTML 4.0 specs

根据HTML 4.0规范

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])、连字符("-")、下划线("_")、冒号(":")和句点(".")。

#5


2  

For purposes of valid html aka xhtml, Philippe is correct. No spaces or special characters (certainly none that require escaping) for id attributes. Just the 52 character alphabet, upper and lower case, numerals 0-9, hyphens ("-"), underscores ("_"), colons (":"), and periods (".")

对于有效的html即xhtml, Philippe是正确的。id属性没有空格或特殊字符(当然没有需要转义的字符)。只有52个字符字母表,大写和小写,数字0-9,连字符(“-”),下划线(“_”),冒号(“:”)和句点(“。”)