How can I select an element with multiple classes in jQuery?
I want to select all the elements that have the two classes a
and b
.
<element class="a b">
So, only the elements that have both classes.
When I use $(".a, .b")
it gives me the union, but I want the intersection.
If you want an intersection, just write the selectors together without spaces in between.
$('.a.b')
So for an element that has an ID of a
with classes b
and c
, you would write:
$('#a.b.c')
你可以使用filter()
函数来做到这一点:
$(".a").filter(".b")
For the case
<element class="a">
<element class="b c">
</element>
</element>
You would need to put a space in between .a
and .bc
$('.a .b.c')
链接地址: http://www.djcxy.com/p/2358.html
上一篇: 我如何使用jQuery获取元素的ID?