Can I put logical operators in document.querySelectorAll? If so, how?

纵然是瞬间 提交于 2019-12-04 17:48:01

问题


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:


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"


来源:https://stackoverflow.com/questions/36544941/can-i-put-logical-operators-in-document-queryselectorall-if-so-how

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!