最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I check with jQuery if an element has at least one class, from a list of specified classes? - Stack Overflow

programmeradmin4浏览0评论

Example 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'.

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

Is there a way to filling the blank in the previous line to acplish 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.

Example 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'.

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

Is there a way to filling the blank in the previous line to acplish 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.

Share Improve this question edited Oct 15, 2012 at 13:59 afuzzyllama 6,5485 gold badges49 silver badges64 bronze badges asked Aug 17, 2010 at 16:35 Lokesh DhakarLokesh Dhakar 5,5895 gold badges24 silver badges24 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 8

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:

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

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. 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.

Try it out: http://jsfiddle/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. Calling this on a selection of jQuery elements will refine the selection to those elements that match the selector you provide. Additionally, you could simply only select those elements to begin with.

So either: $('div').filter('.red, .green, .blue');

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

Here's the documentation on using the filter function: http://api.jquery./filter/

And finally here's a demonstration of using it: http://jsfiddle/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. Hopefully that should give you a good idea of your range of options.

Determine if an element has a CSS class with jQuery

http://docs.jquery./Traversing/hasClass

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论