在JQuery中选择具有单个类的元素 [英] Select elements with a single class in JQuery

查看:62
本文介绍了在JQuery中选择具有单个类的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够选择只有一个(给定)类(而不是其他)的所有元素

I want to be able to select all elements that only have a single (given) class (and no other)

例如,让我们说我的页面看起来像这个

for example, let's say my page looks like this

<html>
    <body>
        <div class="a"> </div>
        <div class="a b"></div>
        <div class="c"></div>
    </body>
</html>

我希望能够只选择第一个元素 - 因为它只有一个类a ,没有其他类。

I want to be able to select only the first element - because it has a single class "a", and no other classes.

谢谢

推荐答案

这个怎么样:

$('.a[class="a"]')

或@Hexa支持:

$('div[class="a"]')

http://jsfiddle.net/taYWP/

@Hexa:它实际上是最快的! ;-) http://jsperf.com/attr-vs-attr-and-class

@Hexa: and it's actually the fastest! ;-) http://jsperf.com/attr-vs-attr-and-class

这篇关于在JQuery中选择具有单个类的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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