jQuery:是否有可能从元素中选择只有一个类的元素,最多可能有3个类? [英] jQuery: Is it possible to select elements with only one class from among elements with, potentially, up to 3 classes?
本文介绍了jQuery:是否有可能从元素中选择只有一个类的元素,最多可能有3个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,假设我有以下内容:
For example, say I have the following:
<span class="a b c">Has class a, b, and c</span>
<span class="a">Has class a</span>
<span class="b c">Has class b and c</span>
<span class="a c">Has class a and c</span>
<span class="a c">Has class a and c</span>
<span class="a">Has class a</span>
现在,假设我要选择所有仅类a的元素。$
意味着只选择第二个和最后一个 span
s。
有一个简单的方法吗?
Now, suppose I want to select all elements that have only class a.
Meaning only the second and last span
s would get selected.
Is there a simple way to do this?
推荐答案
$('.a').not('.b, .c')
这将选择类 a
没有 b
或 c
。但是,他们仍然可以拥有 d
, e
等等。如果你真的只想要没有元素,请使用Phil的答案 a
以外的类。
This will select all elements with class a
that do not have b
or c
. However, they could still have class d
, e
, etc. Use Phil's answer if you really only want elements with no class other than a
.
这篇关于jQuery:是否有可能从元素中选择只有一个类的元素,最多可能有3个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文