如何获取多个类的元素
假设我有这个:
<div class="class1 class2"></div>
如何选择这个 div
元素?
document.getElementsByClassName('class1')[0].getElementsByClassName('class2')[0]
那是行不通的。
我知道,在 jQuery 中,它是 $('.class1.class2')
,但我想用普通 JavaScript 选择它。
Say I have this:
<div class="class1 class2"></div>
How do I select this div
element?
document.getElementsByClassName('class1')[0].getElementsByClassName('class2')[0]
That does not work.
I know that, in jQuery, it is $('.class1.class2')
, but I'd like to select it with vanilla JavaScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
AND (两个类)
OR (至少一个类)
XOR (一个类别,但不是另一个类别)
NAND (不是两个类别)
NOR (不是两个类别中的任何一个)类)
AND (both classes)
OR (at least one class)
XOR (one class but not the other)
NAND (not both classes)
NOR (not any of the two classes)
它实际上与 jQuery 非常相似:
MDN Doc getElementsByClassName
It's actually very similar to jQuery:
MDN Doc getElementsByClassName
具有标准类选择器的 querySelectorAll 也适用于此。
querySelectorAll with standard class selectors also works for this.
正如@filoxo所说,您可以使用
document.querySelectorAll
。如果您知道只有一个元素具有您要查找的类,或者您只对第一个元素感兴趣,则可以使用:
BTW,而
.class1.class2
表示带有 < 的元素em>两个类,.class1 .class2
(注意空格)表示一个层次结构 - 类class2
的元素位于类的元素内>class1
:如果你愿意的话要强制检索直接子级,请使用
>
符号 (.class1 > .class2
):有关选择器的完整信息:
https://www.w3schools.com/jquery/jquery_ref_selectors.asp
As @filoxo said, you can use
document.querySelectorAll
.If you know that there is only one element with the class you are looking for, or you are interested only in the first one, you can use:
BTW, while
.class1.class2
indicates an element with both classes,.class1 .class2
(notice the whitespace) indicates an hierarchy - and element with classclass2
which is inside en element with classclass1
:And if you want to force retrieving a direct child, use
>
sign (.class1 > .class2
):For entire information about selectors:
https://www.w3schools.com/jquery/jquery_ref_selectors.asp
好的,这段代码正是您所需要的:
HTML:
JS:
希望它有帮助! ;)
Okay this code does exactly what you need:
HTML:
JS:
Hope it helps! ;)
html
javascritp 代码
querySelectorAll() 方法返回文档中与指定 CSS 匹配的所有元素选择器,作为静态 NodeList 对象。
NodeList 对象表示节点的集合。可以通过索引号来访问节点。索引从 0 开始。
还可以了解有关 https://www.w3schools 的更多信息。 com/jsref/met_document_queryselectorall.asp
== 谢谢 ==
html
javascritp code
The querySelectorAll() method returns all elements in the document that matches a specified CSS selector(s), as a static NodeList object.
The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.
also learn more about https://www.w3schools.com/jsref/met_document_queryselectorall.asp
== Thank You ==
实际上 @bazzlebrush 的回答和 @filoxo 的评论对我帮助很大。
我需要找到类可能为“zA yO”OR“zA zE”的元素
使用 jquery 我首先选择所需元素的父元素:(
类以“abc”开头的 div 和style != 'display:none')
然后该元素所需的子元素:
完美运行!请注意,您不必执行 document.querySelector 您可以像上面一样传递预先选择的对象。
actually @bazzlebrush 's answer and @filoxo 's comment helped me a lot.
I needed to find the elements where the class could be "zA yO" OR "zA zE"
Using jquery I first select the parent of the desired elements:
(a div with class starting with 'abc' and style != 'display:none')
then the desired children of that element:
works perfectly! note you don't have to do document.querySelector you can as above pass in a pre-selected object.