当元素具有多个类时,jQuery按特定类查找元素 [英] jquery find element by specific class when element has multiple classes

查看:110
本文介绍了当元素具有多个类时,jQuery按特定类查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在从事后端团队在构建中未曾深思熟虑的工作.那给我留下了一个充满div的文档.

So I am working on something that wasn't well thought out in the build from the backend team. That leaves me with a document full of divs.

我正在做的事情是从需要单击的元素上回滚,获取父容器,然后在父容器中找到具有class="alert-box warn"class="alert-box dead"等的元素...本质上,我正在尝试在每个元素上使用多个类选择器.当我尝试仅找到alert-box时,它似乎无法正常工作.我假设是因为它有warn,死角,``很好,等等...

What I am doing is rolling back from the element I need to click on, get the parent container then find an element within the parent which has class="alert-box warn", class="alert-box dead", etc... Essentially, I'm trying to use multiple class selectors on each element. When I try to find just alert-box it doesn't seem to be working right. I'm assuming because it has warn,dead, ``fine, etc...

如何找到alert-box*或等同于通配符的概念?

How can I find just alert-box* or equivalent to a wildcard concept?

推荐答案

您可以组合这样的选择器

You can combine selectors like this

$(".alert-box.warn, .alert-box.dead");

或者,如果您想使用通配符,请使用属性包含选择器

Or if you want a wildcard use the attribute-contains selector

$("[class*='alert-box']");

注意:使用上面的选择器时,最好知道元素类型或标签.了解标签可以提高选择器的效率.

Note: Preferably you would know the element type or tag when using the selectors above. Knowing the tag can make the selector more efficient.

$("div.alert-box.warn, div.alert-box.dead");
$("div[class*='alert-box']");

这篇关于当元素具有多个类时,jQuery按特定类查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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