gpt4 book ai didi

javascript - querySelectorAll 语法无效

转载 作者:行者123 更新时间:2023-12-03 19:12:28 25 4
gpt4 key购买 nike

尝试在错误控制台中执行这 2 个代码块:

第一个。输出节点列表。

var selector = "*[data-type=day][data-day='23']";
var a = document.querySelectorAll( selector );
alert( a );

第二个。产生错误。

var selector = "*[data-type=day][data-day=23]";
var a = document.querySelectorAll( selector );
alert( a );

为什么 data-type=day 是正确的语法而 data-day=23 不是?属性值是否应始终包装为 data-type='day'

这是在第二种情况下引发的异常:

[Exception... "An invalid or illegal string was specified" code: "12" nsresult: "0x8053000c (SyntaxError)" ]

最佳答案

浏览器紧跟着the specification其中说:

Attribute values must be CSS identifiers or strings.

identifiers :

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 (_); they cannot start with a digit

所以23(以数字开头)必须表示为a string :

Strings can either be written with double quotes or with single quotes

关于javascript - querySelectorAll 语法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13024879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com