我可以在document.querySelectorAll中放置逻辑运算符吗?如果是这样,怎么样? [英] Can I put logical operators in document.querySelectorAll? If so, how?

查看:110
本文介绍了我可以在document.querySelectorAll中放置逻辑运算符吗?如果是这样,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想在 p 中找到所有 div 元素和 span code>。是否有可能在一个`querySelectorAll'调用中得到我想要的所有内容?

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?

从概念上讲,它应该类似于 document.querySelectorAll(div | p span)(如果 | 表示)。

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

推荐答案

是的。您可以使用CSS中允许的相同逻辑运算符:

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

OR:带逗号的链选择器

OR: chain selectors with commas

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

AND:没有空格的链选择器

AND: chain selectors without whitespace

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

NOT::not() -selector

NOT: :not()-selector

document.querySelectorAll('div:not(.myClass)');
// selects divs that do not have the class "myClass"

这篇关于我可以在document.querySelectorAll中放置逻辑运算符吗?如果是这样,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆