如何使用 jQuery 检查某个元素是否至少具有指定类列表中的一个类?
示例 html:
<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>
<div class="apple"></div>
我想循环遍历 div 并过滤掉它们,如果它们没有“红色”、“绿色”或“蓝色”类。
var onlyColorDivs = $('div').hasClass( __________ );
有没有办法填充上一行中的空白来完成此操作。或者我是否必须将列表颜色类放入数组中并执行循环?
如果需要任何说明,请告诉我。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
所有的答案都很好而且很恰当。但是,如果您经常需要这样的函数,您也可以对
hasClass
方法进行 Monkey Patch 来执行您想要的操作:然后您可以像这样使用它:
JS Bin 演示
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:Then you can use it like this:
Demo on JS Bin
使用传统的选择方式有什么问题吗?
如果您已经有一个要过滤的集合,例如
您可以使用提到的 filter 函数,但这会比较慢,因此如果您有选择,请在单个选择器中对其进行编码。
没有必要为已支持的功能(可通过不同的语法使用)扩展 jQuery。如果您坚持使用数组作为输入,加入它 在选择器中:
Is there something wrong with using the conventional selection way?
If you already have a collection you want to filter, say
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:
这将仅选择至少具有这 3 个类之一的
元素。
尝试一下: http://jsfiddle.net/gADQn/
This will select only
<div>
elements that have at least one of those 3 classes.Try it out: http://jsfiddle.net/gADQn/
应该做这项工作。
或者只是最初选择它们:
Should do the job.
Or just initially select them:
.filter(selector) 是您正在寻找的函数。在选择的 jQuery 元素上调用此方法会将选择细化为与您提供的选择器匹配的元素。此外,您可以简单地只选择这些元素来开始。
所以要么:
$('div').filter('.red, .green, .blue');
或者:
$('div.red, div.green, div.blue ');
以下是使用过滤器功能的文档: http://api.jquery.com /filter/
最后是使用它的演示: http://jsfiddle.net/Etb7R/< /a>
这个小脚本选择所有
并用其类属性的文本填充它们,然后过滤到仅那些类为 'red'、'green' 或 ' blue' 并应用一个额外的类来突出显示它们。希望这能让您对自己的选择范围有一个很好的了解。
.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.com/filter/
And finally here's a demonstration of using it: 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. Hopefully that should give you a good idea of your range of options.使用 jQuery 确定元素是否具有 CSS 类
http://docs.jquery.com/Traversing/hasClass
Determine if an element has a CSS class with jQuery
http://docs.jquery.com/Traversing/hasClass