简体   繁体   English

111 是有效的 HTML id 属性还是 document.querySelector() 和 document.querySelectorAll() 正确抛出语法错误?

[英]Is 111 a valid HTML id attribute or are document.querySelector() and document.querySelectorAll() correctly throwing syntax errors?

Given给定的

 const div = document.createElement("div"); div.id = 111; document.body.appendChild(div); try { console.log(document.querySelector("#111")); } catch(e) { console.error(e); /* Chromium: DOMException: Failed to execute 'querySelector' on 'Document': '#111' is not a valid selector. Firefox SyntaxError: '#111' is not a valid selector */ } try { console.log(document.querySelectorAll("#111")); } catch(e) { console.error(e); /* Chromium: DOMException: Failed to execute 'querySelector' on 'Document': '#111' is not a valid selector. Firefox SyntaxError: '#111' is not a valid selector */ }

however, document.getElementById() returns the element但是, document.getElementById()返回元素

 const div = document.createElement("div"); div.id = 111; document.body.appendChild(div); try { console.log(document.getElementById("111")); } catch(e) { console.error(e); }

References:参考资料:

6.2 SGML basic types 6.2 SGML 基本类型

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

7.5.2 Element identifiers: the id and class attributes 7.5.2 元素标识符:id 和 class 属性

Attribute definitions属性定义

id = name [CS] This attribute assigns a name to an element. id = name [CS] 此属性为元素分配名称。 This name must be unique in a document.此名称在文档中必须是唯一的。

DOM - Living Standard DOM - 生活标准

While this specification defines requirements for class, id, and slot attributes on any element, it makes no claims as to whether using them is conforming or not.虽然本规范定义了对任何元素的 class、id 和 slot 属性的要求,但它没有声明使用它们是否符合要求。

HTML - Living Standard HTML - 生活标准

The id attribute specifies its element's unique identifier (ID) . id 属性指定其元素的唯一标识符 (ID)

There are no other restrictions on what form an ID can take;对于 ID 可以采用的形式没有其他限制; in particular, IDs can consist of just digits, start with a digit, start with an underscore, consist of just punctuation, etc.特别是,ID 可以仅包含数字、以数字开头、以下划线开头、仅包含标点符号等。

Document.querySelector()

Throws a SYNTAX_ERR exception if the specified group of selectors is invalid.如果指定的选择器组无效,则抛出 SYNTAX_ERR 异常。

Document.querySelectorAll() Document.querySelectorAll()

Throws a SYNTAX_ERR exception if the specified group of selectors is invalid.如果指定的选择器组无效,则抛出 SYNTAX_ERR 异常。


Is "111" a valid HTML id attribute or are document.querySelector() and document.querySelectorAll() correctly throwing syntax errors? "111"是有效的 HTML id 属性还是document.querySelector()document.querySelectorAll()正确抛出语法错误?

According to MDN about the HTML id attribute:根据 MDN 关于 HTML id属性:

This attribute's value must not contain whitespace (spaces, tabs etc.).此属性的值不得包含空格(空格、制表符等)。

That seems to be the only restriction.这似乎是唯一的限制。 They have a note that HTML4 was more strict, but that's it.他们注意到 HTML4 更严格,但仅此而已。

According to the spec :根据规范

The value must not contain any ASCII whitespace.该值不得包含任何 ASCII 空格。

That means that 111 is a valid value for the HTML id attribute.这意味着111是 HTML id属性的有效值。

querySelector and querySelectorAll however use CSS selectors, which are more strict:然而querySelectorquerySelectorAll使用更严格的 CSS 选择器:

An ID selector consists of a # , followed by a valid identifier . ID 选择器由一个#和一个有效的 标识符组成

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_);在 CSS 中,标识符(包括选择器中的元素名称、类和 ID)只能包含字符 [a-zA-Z0-9] 和 ISO 10646 字符 U+00A0 及更高,加上连字符 (-) 和下划线 ( _); they cannot start with a digit , two hyphens, or a hyphen followed by a digit.它们不能以一个数字、两个连字符或一个连字符后跟一个数字开头。 Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item).标识符还可以包含转义字符和任何 ISO 10646 字符作为数字代码(参见下一项)。 For instance, the identifier "B&W?"例如,标识符“B&W?” may be written as "B\\&W\\?"可以写成“B\\&W\\?” or "B\\26 W\\3F".或“B\\26 W\\3F”。

That means that #111 is not a valid CSS selector.这意味着#111不是有效的 CSS 选择器。 Instead, use:相反,使用:

document.querySelector('[id="111"]')

To answer your question directly:要直接回答您的问题:

Is "111" a valid HTML id attribute or are document.querySelector() and document.querySelectorAll() correctly throwing syntax errors? "111"是有效的 HTML id 属性还是document.querySelector()document.querySelectorAll()正确抛出语法错误?

"111" is a valid attribute, and the syntax error are thrown correctly. "111"是一个有效的属性,语法错误被正确抛出。

Just ran into the problem that I cannot use GUIDs as ID.刚刚遇到了我不能使用 GUID 作为 ID 的问题。 If the GUID starts with a letter, it works.如果 GUID 以字母开头,则它有效。 If the GUID starts with a digit, it will not work.如果 GUID 以数字开头,它将不起作用。 Maybe one documentation says that a GUID can start with a digit, but not in all case ...也许一个文档说 GUID 可以以数字开头,但并非在所有情况下......

-> The answer in 2020 is: IDs should start with a letter, so that they work reliably everywhere - no matter if CSS, JavaScript - no matter which browser. -> 2020 年的答案是:ID 应该以字母开头,以便它们在任何地方都能可靠地工作——无论是 CSS、JavaScript——无论是哪种浏览器。

Now I simply put a G- in front of my GUIDs.现在我只需在我的 GUID 前面放一个 G-。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM