jQuery 检查所有项目是否具有相同的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7485066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if all items have the same class
提问by John
I built a FAQ page with the option to hide and show the content underneath each question. I have a "Expand all" functionality that let the user display all the questions onclick. When a question is expanded it gets a class of "selected".
我建立了一个常见问题页面,其中包含隐藏和显示每个问题下方内容的选项。我有一个“全部展开”功能,可以让用户在点击时显示所有问题。当一个问题被扩展时,它会得到一个“选择”类。
I am trying to change the "Expand All" status when all questions (LIs) are expanded.
当所有问题 (LI) 都展开时,我试图更改“全部展开”状态。
How can I check that all the LI have the CLASS "selected" at the same time?
如何检查所有 LI 是否同时“选择”了 CLASS?
I use the EACH method to get the LIs and their CLASS.
我使用 EACH 方法来获取 LI 及其 CLASS。
Thanks in advance
提前致谢
回答by Xion
You can probably count the list items with selected
class against all list items:
您可能可以selected
针对所有列表项计算带有class 的列表项:
if ($("#questions li.selected").length == $("#questions li").length) {
// all list items are selected
}
#questions
is the element that contains your list and of course it might be different in your code, but you should get the idea.
#questions
是包含您的列表的元素,当然它可能在您的代码中有所不同,但您应该明白这一点。
回答by ipr101
$("li:not(.selected)").length
Would give you the number of <li>
s that do not have the 'selected' class. If this figure was zero you could run your logic.
会给你<li>
没有 'selected' 类的s的数量。如果这个数字为零,你可以运行你的逻辑。
回答by Dennis
Select all list items, filter out the items belonging to a certain class and then determine whether or not there are any left over:
选择所有列表项,过滤掉属于某个类的项,然后判断是否还有剩余:
if($("li").not(".className").length > 0 ) {
//code
}
回答by James Allardice
You could compare the number of li
elements to the number of li
elements with the class "selected". If those numbers are the same, then all the li
elements have that class:
您可以将li
元素数量li
与具有“selected”类的元素数量进行比较。如果这些数字相同,则所有li
元素都具有该类:
if($("li").length == $("li.selected").length) {
//All li elements have class selected
}
You can do this at any point, it does not have to go inside an each
loop.
您可以随时执行此操作,不必进入each
循环。
回答by Steve Mather
Or you could try this with size()
或者你可以试试这个 size()
if( $("li.success").size() == $("li").size() ){
//return true
}
回答by Jan
When i understnad you right, you want to set the class selected
to all of your li elements when clicking on a ShowAll button?
当我理解您时,您想selected
在单击 ShowAll 按钮时将类设置为所有 li 元素吗?
You don't need to iterate yourself over all li
elements.
您不需要对所有li
元素进行迭代。
Just select them and call addClass
on them:
只需选择它们并调用addClass
它们:
$('li').addClass('selected');
回答by Mika Andrianarijaona
I am not sure to understand the problem but the check if a jQuery object has a class, you use .hasClass().
我不确定是否理解这个问题,但是检查一个 jQuery 对象是否有一个类,你使用.hasClass()。