Jquery $.each 选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3450635/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:29:02  来源:igfitidea点击:

Jquery $.each selector

jqueryjquery-selectorsprototypejseach

提问by Steffi

I would like to know what $.each()stands for in jquery,
What is it selecting?

我想知道$.each()在 jquery 中代表
什么,它选择什么?

Is there an equivalent in prototype?

原型中有等价物吗?

回答by user113716

$.each()isn't selecting anything. It is just a utility to iterate over a collection.

$.each()没有选择任何东西。它只是一个迭代集合的实用程序。

When you do:

当你这样做时:

$('someSelector').each(function() {
    // do something
});

jQuery is internally calling:

jQuery 在内部调用:

jQuery.each( this, callback, args );

...with thisrepresenting the matched set.

...this代表匹配的集合。

http://github.com/jquery/jquery/blob/master/src/core.js#L231

http://github.com/jquery/jquery/blob/master/src/core.js#L231

You could just as easily call it yourself manually.

您可以轻松地自己手动调用它。

jQuery.each( $('someSelector'), function() {
    // do something
});

回答by Adriaan Stander

I think you should rather look at

我认为你应该看看

jQuery.each()

jQuery.each()

From the documentation

从文档

The $.each() function is not the same as .each(),which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is a map (JavaScript object) or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this keyword.)

$.each() 函数与 .each() 不同,后者用于专门遍历 jQuery 对象。$.each() 函数可用于迭代任何集合,无论是映射(JavaScript 对象)还是数组。在数组的情况下,回调每次都会传递一个数组索引和相应的数组值。(也可以通过 this 关键字访问该值。)

回答by Barlow Tucker

jQuery uses eachin two ways:

jQueryeach有两种使用方式:

The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.

.each() 方法旨在使 DOM 循环构造简洁且不易出错。当被调用时,它遍历作为 jQuery 对象一部分的 DOM 元素。每次回调运行时,都会传递当前循环迭代,从 0 开始。更重要的是,回调是在当前 DOM 元素的上下文中触发的,因此关键字 this 指的是该元素。

refer .each()

参考 .each()

The $.each() function is not the same as .each(), which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is a map (JavaScript object) or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this keyword.)

$.each() 函数与 .each() 不同,后者用于专门遍历 jQuery 对象。$.each() 函数可用于迭代任何集合,无论是映射(JavaScript 对象)还是数组。在数组的情况下,回调每次都会传递一个数组索引和相应的数组值。(也可以通过 this 关键字访问该值。)

$.each()

$.each()

回答by Gadde

It is used to iterate exclusively, over a jQuery object. It can be used to iterate over any collection, see the documentation for jQuery.each.

它用于在 jQuery 对象上进行独占迭代。它可以用于迭代任何集合,看到的文档jQuery.each

回答by Tarun Gupta

The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value.) The method returns its first argument, the object that was iterated. 1 2 3

$.each() 函数与 $(selector).each() 不同,后者用于专门遍历 jQuery 对象。$.each() 函数可用于迭代任何集合,无论是对象还是数组。在数组的情况下,回调每次都会传递一个数组索引和相应的数组值。(该值也可以通过 this 关键字访问,但 Javascript 将始终将 this 值包装为一个对象,即使它是一个简单的字符串或数字值。)该方法返回它的第一个参数,即被迭代的对象。1 2 3

$.each([52, 97], function(index, value) {
alert(index + ': ' + value);
});

This produces two messages:

这会产生两条消息:

0: 52 1: 97

0:52 1:97