jquery $('.class').each() 有多少项?

发布于 2024-10-19 09:32:13 字数 43 浏览 1 评论 0原文

当使用jquery选择器循环一组项目时,有没有办法找出集合中有多少项目?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ㄟ。诗瑗 2024-10-26 09:32:13

如果您使用链式语法:

$(".class").each(function() {
    // ...
});

...我认为 each 函数中的代码没有任何(合理的)方法来知道有多少项。 (不合理的方法会涉及重复选择器和使用索引。)

但是让集合可用于您在 each< 中调用的函数是很容易的。 /代码>。这是一种方法:

var collection = $(".class");
collection.each(function() {
    // You can access `collection.length` here.
});

作为一个有点复杂的选项,您可以将 jQuery 对象转换为数组,然后使用该数组的 forEach。传递给 forEach 回调的参数是正在访问的条目(jQuery 为您提供 this第二个 参数),该条目的索引以及您调用它的数组:

$(".class").get().forEach(function(entry, index, array) {
    // Here, array.length is the total number of items
});

这假设至少有一个模糊现代JavaScript引擎和/或Array#forEach的垫片。

或者就此而言,给自己一个新工具:

// 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);
    });
};

用法:

$(".class").loop(function(element, index, set) {
    // Here, set.length is the length of the set
});

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. (Unreasonable ways would involve repeating the selector and using 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:

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 second argument), the index of that entry, and the array you called it on:

$(".class").get().forEach(function(entry, index, array) {
    // Here, array.length is the total number of items
});

That assumes an at least vaguely modern JavaScript engine and/or a shim for 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
});
顾挽 2024-10-26 09:32:13

使用 .length 属性。它不是一个函数。

alert($('.class').length); // alerts a nonnegative number 

Use the .length property. It is not a function.

alert($('.class').length); // alerts a nonnegative number 
魔法少女 2024-10-26 09:32:13

如果您使用的 jQuery 版本低于 1.8,您可以使用 $('.class').size() ,它接受零个参数。有关 .size() 方法的更多信息,请参阅文档

但是,如果您正在使用(或计划升级)到 1.8 或更高版本,则可以使用 $('.class').length 属性。有关 .length 属性的更多信息,请参阅文档

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 documentation for 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 documentation for more information on .length property.

日记撕了你也走了 2024-10-26 09:32:13

您的意思是 lengthsize() 吗?

参考:http://api.jquery.com/length/

You mean like length or size()?

Refs: http://api.jquery.com/length/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文