javascript - querySelectorAll 语法无效

标签 javascript firefox css-selectors selectors-api

尝试在错误控制台中执行这 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/

相关文章:

javascript - 防止 Firefox 自动完成弹出

android - 如何以非编程方式在 Android 上配置代理排除列表?

python - lxml: cssselect(): AttributeError: 'lxml.etree._Element' 对象没有属性 'cssselect'

C# 获取 Firefox 中所有打开页面的 URL

javascript - CSS - 在这种情况下删除类的更好方法?

css - 游标属性是否应该在有或没有 :hover pseudo-class? 的规则中设置

javascript - ios应用程序后退按钮不保存cookie

javascript - 在 Javascript 中返回图像的数据 URI Base64 字符串的函数

javascript - 使用 javascript 附加 json 文件

javascript - 我的错误控制台中出现未定义的错误