querySelectorAll具有多个条件 [英] querySelectorAll with multiple conditions
问题描述
是否可以使用多个不相关的条件通过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 $ c $的列表c> (和)的元素具有类
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屋!