HTML 属性名称中允许使用哪些字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/925994/
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
What characters are allowed in an HTML attribute name?
提问by TheFlash
In HTML attribute name=valuepairs, what are the characters allowed for the 'name' portion? ..... Looking at some common attributesit 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)、连字符 (-) 和句号 (.) ... 有没有这方面的规范?
采纳答案by Alohci
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.
它表示除制表符、换行符、换页符、空格、实线、大于号、引号、撇号和等号之外的所有字符都将被视为属性名称的一部分。就个人而言,我不会尝试推动这种边缘情况。
回答by S.Lott
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)*
回答by Daan
Maybe I'm missing something, but I believe the question is based on a false assumption. In HTML, attributes are strictly definedaccording to a fixed specification. If you 'make up' your own attribute names, you are no longer writing valid HTML.
也许我错过了一些东西,但我相信这个问题是基于一个错误的假设。在 HTML 中,属性是根据固定规范严格定义的。如果您“编造”自己的属性名称,则不再编写有效的 HTML。
回答by Clayton Gulick
Since this question was asked, the web has evolved quite a bit. It's likely that authors of Web Components (custom elements) are landing here trying to learn what valid names can be used when defining attributes on custom elements.
自从提出这个问题以来,网络已经发展了很多。Web 组件(自定义元素)的作者很可能正在尝试了解在自定义元素上定义属性时可以使用哪些有效名称。
There are several answers here that are partially correct, so I'm going to try to aggregate them and update them based on recent specs.
这里有几个答案是部分正确的,所以我将尝试汇总它们并根据最近的规范更新它们。
First, in HTML5, attribute names can start with most characters and are much more permissive than in previous versions of HTML. @S.Lott 's answer is correct for HTML 2 and XHTML, but not for HTML5.
首先,在 HTML5 中,属性名称可以以大多数字符开头,并且比以前的 HTML 版本更加宽松。@S.Lott 的答案对于 HTML 2 和 XHTML 是正确的,但对于 HTML5 则不正确。
For HTML5: (spec)
对于 HTML5:(规范)
Attribute names must consist of one or more characters other than the space characters, U+0000 NULL, U+0022 QUOTATION MARK ("), U+0027 APOSTROPHE ('), U+003E GREATER-THAN SIGN (>), U+002F SOLIDUS (/), and U+003D EQUALS SIGN (=) characters, the control characters, and any characters that are not defined by Unicode. In the HTML syntax, attribute names, even those for foreign elements, may be written with any mix of lower- and uppercase letters that are an ASCII case-insensitive match for the attribute's name.
属性名称必须包含一个或多个除空格字符以外的字符、U+0000 NULL、U+0022 QUOTATION MARK (")、U+0027 APOSTROPHE (')、U+003E GREATER-THAN SIGN (>)、U+ 002F SOLIDUS (/) 和 U+003D EQUALS SIGN (=) 字符、控制字符和任何未由 Unicode 定义的字符。在 HTML 语法中,属性名称,甚至是外部元素的属性名称,都可以用任何小写和大写字母的混合,是属性名称的 ASCII 不区分大小写匹配。
That being said, other commenters here are correct, when using an attribute on a built-in element that's not in it's list of valid attributes, you're technically violating the spec. Browser authors have a lot of tolerance for this though, so in practice it doesn't do (much?) harm. A lot of libraries exploit this to enhance regular HTML tags, which causes some confusion, since it's technically not valid HTML. HTML5 provides a mechanism for custom data in attributes by using the data- attribute naming convention.
话虽如此,这里的其他评论者是正确的,当在内置元素上使用不在其有效属性列表中的属性时,您在技术上违反了规范。浏览器作者对此有很大的容忍度,因此实际上它不会造成(很多?)伤害。许多库利用它来增强常规 HTML 标签,这会引起一些混乱,因为它在技术上不是有效的 HTML。HTML5 通过使用数据属性命名约定为属性中的自定义数据提供了一种机制。
These rules are different for custom elements.
这些规则对于自定义元素是不同的。
Custom element authors are welcome to implement any sort of attribute they like to their element, the names of the attributes are more restrictive than HTML5 though. In fact, the specrequires that the attribute name follow the XML Namerestrictions:
欢迎自定义元素作者为他们的元素实现他们喜欢的任何类型的属性,但属性的名称比 HTML5 更严格。事实上,规范要求属性名称遵循XML Name限制:
The ASCII symbols and punctuation marks, along with a fairly large group of Unicode symbol characters, are excluded from names because they are more useful as delimiters in contexts where XML names are used outside XML documents; providing this group gives those contexts hard guarantees about what cannot be part of an XML name. The character #x037E, GREEK QUESTION MARK, is excluded because when normalized it becomes a semicolon, which could change the meaning of entity references.
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)*
ASCII 符号和标点符号以及相当大的一组 Unicode 符号字符被排除在名称之外,因为它们在 XML 名称在 XML 文档之外使用的上下文中作为分隔符更有用;提供这个组为那些上下文提供了关于什么不能成为 XML 名称的一部分的硬保证。字符#x037E(希腊问号)被排除在外,因为标准化后它会变成分号,这可能会改变实体引用的含义。
名称和令牌
[4] NameStartChar ::= ":" | [AZ] | "_" | [az] | [#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] 名称 ::= NameStartChar (NameChar)*
[6] 名称 ::= 名称 (#x20 名称)*
[7] Nmtoken ::= (NameChar)+
[8] Nmtoken ::= Nmtoken (#x20 Nmtoken)*
So, for custom element names you can use upper/lower alphanumeric, "_" underscore, ":" colon, or any of the unicode characters called out in the spec, as a start character, then dashes "-", dots ".", alpha etc... as body characters.
因此,对于自定义元素名称,您可以使用大写/小写字母数字、“_”下划线、“:”冒号或规范中提及的任何 unicode 字符作为起始字符,然后使用破折号“-”、点“。 "、alpha 等...作为正文字符。