Luk*_*tic 1 javascript google-chrome-extension tampermonkey
https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
此处,属性选择器用于返回包含在 ID 为“userlist”的列表中的列表项列表,该列表项的值为“1”的“data-active”属性
var container = document.querySelector("#userlist");
var matches = container.querySelectorAll("li[data-active=1]");
Run Code Online (Sandbox Code Playgroud)
但是当我尝试
var matches = document.querySelectorAll('div[data-id=7821549]');
Run Code Online (Sandbox Code Playgroud)
我得到:
DOMException: Failed to execute 'querySelectorAll' on 'Document': 'div[data-id=7821549]' is not a valid selector.
Run Code Online (Sandbox Code Playgroud)
这是我试图获得的 div:
<div class="cell cell--event-list cell--odds js-event js-event-odds js-event-odds-7821549 js-event-status-finished" data-id="7821549">
Run Code Online (Sandbox Code Playgroud)
示例选择器"li[data-active=1]"在我的 Chromium 浏览器上引发了类似的错误,因为属性选择器的值是一个数字。即使是#22用于有效 HTML5 ID 的简单数字 ID 选择器(如)也不适用于querySelectormethod。
用引号包裹属性选择器的值可以解决这个问题: 'div[data-id="7821549"]'