querySelector 问题 'tbody > tr[data-index=-1]'

Issue with querySelector 'tbody > tr[data-index=-1]'

我有以下查询选择器。使用 Chrome 38 我得到一个 "SYNERR: Is not a valid selector" ..现在有趣的是,在 CSS 中它工作正常。

tbody > tr[data-index=-1]

知道为什么 JavaScript 不喜欢这条路吗?

问题是,在attribute selectors

Attribute values must be identifiers or strings

但是 -1 不是有效的 identifier(强调我的):

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, two hyphens, or a hyphen followed by a digit.

因此,您必须使用 string:

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

例如,

tbody > tr[data-index="-1"]