简体   繁体   English

如果一个元素至少有一个类,从指定的类列表中如何检查jQuery?

[英]How do I check with jQuery if an element has at least one class, from a list of specified classes?

Example html: 示例html:

<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>

<div class="apple"></div>

I want to loop through the divs and filter them out if they don't have a class of either 'red', 'green', or 'blue'. 如果他们没有“红色”,“绿色”或“蓝色”类,我想循环遍历div并过滤掉它们。

var onlyColorDivs = $('div').hasClass( __________ );

Is there a way to filling the blank in the previous line to accomplish this. 有没有办法填写上一行中的空白来完成此任务。 Or am I going to have to put my list color classes in an array and do a loop? 或者我是否必须将列表颜色类放在数组中并进行循环?

Let me know if any clarification is needed. 如果需要澄清,请与我们联系。

All the answers are great and are appropriate. 所有的答案都很棒,也很合适。 But, if you need a function like this a lot, you could also Monkey Patch the hasClass method to do what you want: 但是,如果您需要这样的函数,您还可以使用Monkey Patch的hasClass方法来执行您想要的操作:

var _hasClass = $.fn.hasClass;
$.fn.hasClass = function (classNames) {
  if (!classNames || typeof classNames === "string" ) {
    return _hasClass.call(this, classNames); // Default behavior
  } else {
    // Take array and parse it for the filter method
    var classes = '.' + classNames.join(', .');
    return this.filter(classes).length > 0;
  }
};

Then you can use it like this: 然后你可以像这样使用它:

$("div").hasClass(['red','green','blue']);
// or
$("div").hasClass('green');

Demo on JS Bin JS Bin上的演示

Is there something wrong with using the conventional selection way? 使用传统的选择方式有什么问题吗?

$(".red, .green, .blue");

If you already have a collection you want to filter, say 如果您已经有想要过滤的集合,请说

$("div");

You can use the filter function as mentioned, but that would be slower, so if you have a choice, code it in a single selector. 您可以使用上面提到的过滤器功能,但这样会更慢,因此如果您有选择,请在单个选择器中对其进行编码。

Extending jQuery for an already supported functionality that's available with a different syntax is unnecessary. 不必使用不同语法扩展jQuery以支持已经支持的功能。 If you insist on using an array as input, it would be faster to join it in a selector: 如果你坚持使用数组作为输入,那么将它加入选择器会更快:

var classes = new Array("red", "green", "blue");
$("div." + classes.join(",div."));

This will select only <div> elements that have at least one of those 3 classes. 这将仅选择具有这3个类中的至少一个的<div>元素。

Try it out: http://jsfiddle.net/gADQn/ 试一试: http //jsfiddle.net/gADQn/

var onlyColorDivs = $('div.red, div.green, div.blue');
.filter('.red,.green,.blue')

Should do the job. 应该做的工作。

Or just initially select them: 或者只是最初选择它们:

$('.red,.green,.blue', context)

.filter(selector) is the function you are looking for. .filter(selector)是您正在寻找的功能。 Calling this on a selection of jQuery elements will refine the selection to those elements that match the selector you provide. 在选择的jQuery元素上调用此选项将优化选择与您提供的选择器匹配的元素。 Additionally, you could simply only select those elements to begin with. 此外,您只需选择那些元素即可。

So either: $('div').filter('.red, .green, .blue'); 所以要么: $('div').filter('.red, .green, .blue');

Or: $('div.red, div.green, div.blue'); 或者: $('div.red, div.green, div.blue');

Here's the documentation on using the filter function: http://api.jquery.com/filter/ 以下是使用过滤功能的文档: http//api.jquery.com/filter/

And finally here's a demonstration of using it: http://jsfiddle.net/Etb7R/ 最后这里是使用它的演示: http//jsfiddle.net/Etb7R/

This little script selects all <div> s and fills them with the text of their class attribute, then filters down to only those with the classes 'red', 'green', or 'blue' and applies an additional class to highlight them. 这个小脚本选择所有<div>并用它们的class属性的文本填充它们,然后过滤到只有那些类为“red”,“green”或“blue”的类,并应用一个额外的类来突出显示它们。 Hopefully that should give you a good idea of your range of options. 希望这应该会让您对您的选择范围有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM