如何获取多个类的元素

发布于 2024-12-01 10:33:30 字数 327 浏览 5 评论 0原文

假设我有这个:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

仄言 2024-12-08 10:33:30

AND (两个类)

var list = document.getElementsByClassName("class1 class2");
var list = document.querySelectorAll(".class1.class2");

OR (至少一个类)

var list = document.querySelectorAll(".class1,.class2");

XOR (一个类别,但不是另一个类别)

var list = document.querySelectorAll(".class1:not(.class2),.class2:not(.class1)");

NAND (不是两个类别)

var list = document.querySelectorAll(":not(.class1),:not(.class2)");

NOR (不是两个类别中的任何一个)类)

var list = document.querySelectorAll(":not(.class1):not(.class2)");

AND (both classes)

var list = document.getElementsByClassName("class1 class2");
var list = document.querySelectorAll(".class1.class2");

OR (at least one class)

var list = document.querySelectorAll(".class1,.class2");

XOR (one class but not the other)

var list = document.querySelectorAll(".class1:not(.class2),.class2:not(.class1)");

NAND (not both classes)

var list = document.querySelectorAll(":not(.class1),:not(.class2)");

NOR (not any of the two classes)

var list = document.querySelectorAll(":not(.class1):not(.class2)");
梦里泪两行 2024-12-08 10:33:30

它实际上与 jQuery 非常相似:

document.getElementsByClassName('class1 class2')

MDN Doc getElementsByClassName

It's actually very similar to jQuery:

document.getElementsByClassName('class1 class2')

MDN Doc getElementsByClassName

夏末的微笑 2024-12-08 10:33:30

具有标准类选择器的 querySelectorAll 也适用于此。

document.querySelectorAll('.class1.class2');

querySelectorAll with standard class selectors also works for this.

document.querySelectorAll('.class1.class2');
溇涏 2024-12-08 10:33:30

正如@filoxo所说,您可以使用document.querySelectorAll

如果您知道只有一个元素具有您要查找的类,或者您只对第一个元素感兴趣,则可以使用:

document.querySelector('.class1.class2');

BTW,而 .class1.class2 表示带有 < 的元素em>两个类,.class1 .class2(注意空格)表示一个层次结构 - 类class2的元素位于类的元素内>class1

<div class='class1'>
  <div>
    <div class='class2'>
      :
      :

如果你愿意的话要强制检索直接子级,请使用 > 符号 (.class1 > .class2):

<div class='class1'>
  <div class='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:

document.querySelector('.class1.class2');

BTW, while .class1.class2 indicates an element with both classes, .class1 .class2 (notice the whitespace) indicates an hierarchy - and element with class class2 which is inside en element with class class1:

<div class='class1'>
  <div>
    <div class='class2'>
      :
      :

And if you want to force retrieving a direct child, use > sign (.class1 > .class2):

<div class='class1'>
  <div class='class2'>
    :
    :

For entire information about selectors:
https://www.w3schools.com/jquery/jquery_ref_selectors.asp

泛滥成性 2024-12-08 10:33:30

好的,这段代码正是您所需要的:

HTML:

<div class="class1">nothing happens hear.</div>

<div class="class1 class2">This element will receive yout code.</div>

<div class="class1">nothing happens hear.</div>

JS:

function getElementMultipleClasses() {
    var x = document.getElementsByClassName("class1 class2");
    x[0].innerHTML = "This is the element you want";
}
getElementMultipleClasses();

希望它有帮助! ;)

Okay this code does exactly what you need:

HTML:

<div class="class1">nothing happens hear.</div>

<div class="class1 class2">This element will receive yout code.</div>

<div class="class1">nothing happens hear.</div>

JS:

function getElementMultipleClasses() {
    var x = document.getElementsByClassName("class1 class2");
    x[0].innerHTML = "This is the element you want";
}
getElementMultipleClasses();

Hope it helps! ;)

弥繁 2024-12-08 10:33:30

html

<h2 class="example example2">A heading with class="example"</h2>

javascritp 代码

var element = document.querySelectorAll(".example.example2");
element.style.backgroundColor = "green";

querySelectorAll() 方法返回文档中与指定 CSS 匹配的所有元素选择器,作为静态 NodeList 对象。

NodeList 对象表示节点的集合。可以通过索引号来访问节点。索引从 0 开始。

还可以了解有关 https://www.w3schools 的更多信息。 com/jsref/met_document_queryselectorall.asp

== 谢谢 ==

html

<h2 class="example example2">A heading with class="example"</h2>

javascritp code

var element = document.querySelectorAll(".example.example2");
element.style.backgroundColor = "green";

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 ==

铁轨上的流浪者 2024-12-08 10:33:30

实际上 @bazzlebrush 的回答和 @filoxo 的评论对我帮助很大。

我需要找到类可能为“zA yO”OR“zA zE”的元素

使用 jquery 我首先选择所需元素的父元素:(

类以“abc”开头的 div 和style != 'display:none')

var tom = $('div[class^="abc"][style!="display: none;"]')[0];                   

然后该元素所需的子元素:

var ax = tom.querySelectorAll('.zA.yO, .zA.zE');

完美运行!请注意,您不必执行 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')

var tom = $('div[class^="abc"][style!="display: none;"]')[0];                   

then the desired children of that element:

var ax = tom.querySelectorAll('.zA.yO, .zA.zE');

works perfectly! note you don't have to do document.querySelector you can as above pass in a pre-selected object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文