Can I put logical operators in document.querySelec

2019-03-25 10:59发布

Let's say I want to find all div elements and span inside p. Is it possible to get all what I want in a single `querySelectorAll" invocation?

Conceptually it should be something like document.querySelectorAll("div | p span") (if | means or).

1条回答
别忘想泡老子
2楼-- · 2019-03-25 11:37

Yes. You can use the same logical operators allowed in CSS:

OR: chain selectors with commas

document.querySelectorAll('div, p span');
// selects divs, and spans in ps

AND: chain selectors without whitespace

document.querySelectorAll('div.myClass');
// selects divs with the class "myClass"

NOT: :not()-selector

document.querySelectorAll('div:not(.myClass)');
// selects divs that do not have the class "myClass"
查看更多
登录 后发表回答