querySelectorAll具有多个条件 [英] querySelectorAll with multiple conditions

查看:610
本文介绍了querySelectorAll具有多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用多个不相关的条件通过querySelectorAll进行搜索?如果有,怎么样?以及如何指定这些是AND还是OR标准?

Is it possible to make a search by querySelectorAll using multiple unrelated conditions? If yes how? And how to specify whether those are AND or OR criteria?

例如:

如何查找全部<一个 querySelectorAll 调用强>形式 s, p 图例?可能吗?

How to find all forms, ps and legends with a single querySelectorAll call? Possible?

推荐答案


是否可以通过进行搜索querySelectorAll 使用多个不相关的条件?

Is it possible to make a search by querySelectorAll using multiple unrelated conditions?

是的,因为 querySelectorAll 接受完整的CSS选择器,CSS具有 选择器组 的概念,它允许您指定多个不相关的选择器。例如:

Yes, because querySelectorAll accepts full CSS selectors, and CSS has the concept of selector groups, which lets you specify more than one unrelated selector. For instance:

var list = document.querySelectorAll("form, p, legend");

...将返回包含表单<的任何元素的列表/ code> p 图例

...will return a list containing any element that is a form or p or legend.

CSS还有另一个概念:基于更多标准的限制。您只需组合选择器的多个方面。例如:

CSS also has the other concept: Restricting based on more criteria. You just combine multiple aspects of a selector. For instance:

var list = document.querySelectorAll("div.foo");

...将返回所有 div )的元素具有类 foo ,忽略其他 div 元素。

...will return a list of all div elements that also (and) have the class foo, ignoring other div elements.

当然,您可以将它们合并:

You can, of course, combine them:

var list = document.querySelectorAll("div.foo, p.bar, div legend");

...这意味着包括任何 div 元素也有 foo 类,任何 p 元素也有 bar class,以及 div 中的任何图例元素。

...which means "Include any div element that also has the foo class, any p element that also has the bar class, and any legend element that's also inside a div."

这篇关于querySelectorAll具有多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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