jquery $('.class').each() 有多少项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5148509/
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
jquery $('.class').each() how many items?
提问by Hailwood
When looping over a group of items using jquery selectors is there a way to find out how many items there are on the collection?
当使用 jquery 选择器循环一组项目时,有没有办法找出集合中有多少项目?
回答by T.J. Crowder
If you're using chained syntax:
如果您使用链式语法:
$(".class").each(function() {
// ...
});
...I don't think there's any (reasonable) way for the code within the each
function to know how many items there are. (Unreasonableways would involve repeating the selector and using index
.)
...我认为each
函数中的代码没有任何(合理的)方法来知道有多少项。(不合理的方法将涉及重复选择器和使用index
。)
But it's easy enough to make the collection available to the function that you're calling in each
. Here's one way to do that:
但是,使集合可用于您正在调用的函数是很容易的each
。这是一种方法:
var collection = $(".class");
collection.each(function() {
// You can access `collection.length` here.
});
As a somewhat convoluted option, you could convert your jQuery object to an array and then use the array's forEach
. The arguments that get passed to forEach
's callback are the entry being visited (what jQuery gives you as this
and as the secondargument), the index of that entry, and the array you called it on:
作为一个有点复杂的选项,您可以将 jQuery 对象转换为数组,然后使用该数组的forEach
. 传递给forEach
的回调的参数是被访问的条目(jQuery 为您提供的this
和作为第二个参数的)、该条目的索引以及您调用它的数组:
$(".class").get().forEach(function(entry, index, array) {
// Here, array.length is the total number of items
});
That assumes an at least vaguelymodern JavaScript engine and/or a shim for Array#forEach
.
这假设至少有一个模糊的现代 JavaScript 引擎和/或Array#forEach
.
Or for that matter, give yourself a new tool:
或者就此而言,给自己一个新工具:
// Loop through the jQuery set calling the callback:
// loop(callback, thisArg);
// Callback gets called with `this` set to `thisArg` unless `thisArg`
// is falsey, in which case `this` will be the element being visited.
// Arguments to callback are `element`, `index`, and `set`, where
// `element` is the element being visited, `index` is its index in the
// set, and `set` is the jQuery set `loop` was called on.
// Callback's return value is ignored unless it's `=== false`, in which case
// it stops the loop.
$.fn.loop = function(callback, thisArg) {
var me = this;
return this.each(function(index, element) {
return callback.call(thisArg || element, element, index, me);
});
};
Usage:
用法:
$(".class").loop(function(element, index, set) {
// Here, set.length is the length of the set
});
回答by darkwisperer
If you are using a version of jQuery that is less than version 1.8 you can use the $('.class').size() which takes zero parameters. See documentationfor more information on .size() method.
However if you are using (or plan to upgrade) to 1.8 or greater you can use $('.class').length property. See documentationfor more information on .length property.
如果您使用的 jQuery 版本低于 1.8,您可以使用 $('.class').size() 它接受零参数。有关.size() 方法的更多信息,请参阅文档。
但是,如果您正在使用(或计划升级)到 1.8 或更高版本,则可以使用 $('.class').length 属性。有关.length 属性的更多信息,请参阅文档。
回答by Matt Ball
Use the .length
property. It is nota function.
使用.length
物业。它不是一个函数。
alert($('.class').length); // alerts a nonnegative number