Skip to content

经验相关 #10

Open
Open
@itboos

Description

@itboos

document.querySelector("#22id"); 不能以数字开头,后面有数字可以支持
这个将导致错误:
document.getElementById('#id22') 这个则可以

VM188:1 Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '#01test' is not a valid selector.
    at <anonymous>:1:10

对于css 2:
该值在元素的主子树中的所有ID中必须是唯一的,并且必须至少包含一个字符。该值不能包含任何空格字符。

身份证可以采用什么形式没有其他限制; 特别是,ID可以只包含数字,以数字开头,以下划线开头,仅包含标点符号等。

但是,querySelector方法使用CSS3选择器来查询DOM,CSS3不支持以数字开头的ID选择器:

在CSS中,标识符(包括元素名称,类和选择器中的ID)只能包含字符[a-zA-Z0-9]和ISO 10646字符U + 00A0及更高版本,以及连字符( - )和下划线_); 他们不能以数字,两个连字符或连字符后跟数字开头。

使用类似于b22ID属性的值,并且您的代码将起作用。
原文链接:
https://stackoverflow.com/questions/37270787/uncaught-syntaxerror-failed-to-execute-queryselector-on-document

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions