在jQuery Select元素中,该元素具有冗余或具有两个相同的类 [英] In jQuery Select element that has redundant or has two of same class

查看:71
本文介绍了在jQuery Select元素中,该元素具有冗余或具有两个相同的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中说:

<div class='a a'></div>
<div class='a a'></div>
<div class='a'></div>
<div class='a a'></div>
<div class='a a'></div>
<div class='a'></div>
<div class='a a'></div>

在jQuery中,如何选择具有两个a类的div元素?

In jQuery, how do I select the div elements with two a classes?

我正在使用 daterangepicker 库,只是想知道我不控制呈现的标签,并且呈现的日历中有两个具有相同类的元素,我想选择这些元素.

I am using the daterangepicker library, just so you know I don't control the rendered tags, and there are elements in the rendered calendar that has two of a same class, I want to select those.

推荐答案

您应该计算类的出现次数,以准确获取所需的项,如下所示.

You should count the occurrences of classes to get exactly the item as you wish like below.

$(".a").filter(function(){
  var classes = $(this).attr("class").split(/\s+/);
  var countClassA = classes.filter(c => c === "a").length;
  return countClassA > 1;
}).addClass('selected');

div{
   padding: 5px;
}

.selected{
  color: red;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='a a'>Double AA</div>
<div class='a a'>Double AA</div>
<div class='a'>Single A</div>
<div class='a a'>Double AA</div>
<div class='a a'>Double AA</div>
<div class='a'>Single A</div>
<div class='a b a'>Double AA</div>

这篇关于在jQuery Select元素中,该元素具有冗余或具有两个相同的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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