HTML属性名称中允许使用哪些字符?

时间:2021-02-07 22:32:46

In HTML attribute name=value pairs, what are the characters allowed for the 'name' portion? ..... Looking at some common attributes it appears that only letters (a-z and A-Z) are used, but what other chars could be allowed as well?... maybe digits (0-9), hyphens (-), and periods (.) ... is there any spec for this?

在HTML属性名称=值对中,“名称”部分允许的字符是什么? .....看一些常见的属性,似乎只使用字母(az和AZ),但也可以允许其他字符?...可能是数字(0-9),连字符( - )和句号(。)......有什么规格吗?

4 个解决方案

#1


45  

It depends what you mean by "allowed". Each tag has a fixed list of attribute names which are valid, and in html they are case insensitive. In one important sense, only these characters in the correct sequence are "allowed".

这取决于你所说的“允许”。每个标记都有一个固定的属性名称列表,这些属性名称是有效的,而在html中它们不区分大小写。在一个重要的意义上,只有正确序列中的这些字符才是“允许的”。

Another way of looking at it, is what characters will browsers treat as a valid attribute name. The best advice here comes from the parser spec of HTML 5, which can be found here: https://html.spec.whatwg.org/multipage/syntax.html#attributes-2

查看它的另一种方式是浏览器将哪些字符视为有效的属性名称。这里最好的建议来自HTML 5的解析器规范,可以在这里找到:https://html.spec.whatwg.org/multipage/syntax.html#attributes-2

It says that all characters except tab, line feed, form feed, space, solidus, greater than sign, quotation mark, apostrophe and equals sign will be treated as part of the attribute name. Personally, I wouldn't attempt pushing the edge cases of this though.

它表示除了制表符,换行符,换页符,空格,固相线,大于符号,引号,撇号和等号之外的所有字符都将被视为属性名称的一部分。就个人而言,我不会尝试推动这种情况。

#2


16  

Assuming you're talking about XHTML, the XML rules apply.

假设您正在谈论XHTML,则适用XML规则。

See http://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name

见http://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name

Names and Tokens

名称和代币

[4]     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]
[4a]    NameChar       ::=      NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
[5]     Name       ::=      NameStartChar (NameChar)*
[6]     Names      ::=      Name (#x20 Name)*
[7]     Nmtoken    ::=      (NameChar)+
[8]     Nmtokens       ::=      Nmtoken (#x20 Nmtoken)*

#3


9  

Maybe I'm missing something, but I believe the question is based on a false assumption. In HTML, attributes are strictly defined according to a fixed specification. If you 'make up' your own attribute names, you are no longer writing valid HTML.

也许我错过了一些东西,但我相信这个问题是基于错误的假设。在HTML中,根据固定的规范严格定义属性。如果您“组成”自己的属性名称,则不再编写有效的HTML。

#4


4  

The values allowed are listed at w3.org. If you add a custom attribute, then you aren't writing HTML any more.

允许的值列在w3.org。如果添加自定义属性,则不再编写HTML。

#1


45  

It depends what you mean by "allowed". Each tag has a fixed list of attribute names which are valid, and in html they are case insensitive. In one important sense, only these characters in the correct sequence are "allowed".

这取决于你所说的“允许”。每个标记都有一个固定的属性名称列表,这些属性名称是有效的,而在html中它们不区分大小写。在一个重要的意义上,只有正确序列中的这些字符才是“允许的”。

Another way of looking at it, is what characters will browsers treat as a valid attribute name. The best advice here comes from the parser spec of HTML 5, which can be found here: https://html.spec.whatwg.org/multipage/syntax.html#attributes-2

查看它的另一种方式是浏览器将哪些字符视为有效的属性名称。这里最好的建议来自HTML 5的解析器规范,可以在这里找到:https://html.spec.whatwg.org/multipage/syntax.html#attributes-2

It says that all characters except tab, line feed, form feed, space, solidus, greater than sign, quotation mark, apostrophe and equals sign will be treated as part of the attribute name. Personally, I wouldn't attempt pushing the edge cases of this though.

它表示除了制表符,换行符,换页符,空格,固相线,大于符号,引号,撇号和等号之外的所有字符都将被视为属性名称的一部分。就个人而言,我不会尝试推动这种情况。

#2


16  

Assuming you're talking about XHTML, the XML rules apply.

假设您正在谈论XHTML,则适用XML规则。

See http://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name

见http://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name

Names and Tokens

名称和代币

[4]     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]
[4a]    NameChar       ::=      NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
[5]     Name       ::=      NameStartChar (NameChar)*
[6]     Names      ::=      Name (#x20 Name)*
[7]     Nmtoken    ::=      (NameChar)+
[8]     Nmtokens       ::=      Nmtoken (#x20 Nmtoken)*

#3


9  

Maybe I'm missing something, but I believe the question is based on a false assumption. In HTML, attributes are strictly defined according to a fixed specification. If you 'make up' your own attribute names, you are no longer writing valid HTML.

也许我错过了一些东西,但我相信这个问题是基于错误的假设。在HTML中,根据固定的规范严格定义属性。如果您“组成”自己的属性名称,则不再编写有效的HTML。

#4


4  

The values allowed are listed at w3.org. If you add a custom attribute, then you aren't writing HTML any more.

允许的值列在w3.org。如果添加自定义属性,则不再编写HTML。