jQuery $.each(selector) 和 $(selector).each() 有什么区别

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

What is the difference between $.each(selector) and $(selector).each()

jqueryeach

提问by marky

What is the difference between this:

这有什么区别:

$.each($('#myTable input[name="deleteItem[]"]:checked').do_something());

and this:

和这个:

$('#myTable input[name="deleteItem[]"]:checked').each(function() { do_something });

The html for the table cell that is being selected and acted upon looks like this:

被选中并对其执行操作的表格单元格的 html 如下所示:

<td width="20px"><input type="checkbox" class="chkDeleteItem" name="deleteItem[]" value="' . $rowItem['itemID'] . '" /></td>

I've gone over the jQuery documentation, but I still don't understand the difference. (Is it me or is that documentation sometimes slightly "nebulous" in clarity of content?)

我已经阅读了 jQuery 文档,但我仍然不明白其中的区别。(是我还是那个文档有时在内容的清晰度方面有点“模糊”?)

Added Info:

添加信息:

Apparently my attempt a generic examples is confusing people! Along with the (previously) missing parenthesis in the first example. :(

显然,我尝试的通用示例使人们感到困惑!连同第一个示例中(以前)缺少的括号。:(

The first example comes from a line in my code that removes the <tbody> for any rows with a checkbox that is checked:

第一个示例来自我的代码中的一行,该行删除了带有选中复选框的任何行的 <tbody> :

$.each($('#classesTable input[name="deleteClasses[]"]:checked').parent().parent().parent().remove());

The second example comes from a situation where I look through the #classesTable for any checked checkboxes and remove its matching item in a dropdown.

第二个示例来自这样一种情况:我查看 #classesTable 以查找任何已选中的复选框并在下拉列表中删除其匹配项。

$('#classesTable input[name="deleteClasses[]"]:checked').each(function(){
    $('#classesList option[value="' + $(this).attr('value') + '"]').remove();
});

I understand that they do two different things, but not to the point that I'd be able to say "I need to use $.each() in this case and .each(function() {}) in another case.

我知道他们做了两件不同的事情,但不能说“我需要在这种情况下使用 $.each() ,在另一种情况下使用 .each(function() {}) 。

Are they interchangeable at all? Only in some cases? Never?

它们完全可以互换吗?仅在某些情况下?绝不?

回答by Phil

Description:

描述:

.eachis an iterator that is used to iterate over only jQuery objects collection while jQuery.each($.each) is a general function for iterating over JavaScript objects and arrays.

.each是一个迭代器,用于仅迭代 jQuery 对象集合,而jQuery.each( $.each) 是用于迭代 JavaScript 对象和数组的通用函数。



Examples

例子

1) Using $.each()function

1) 使用$.each()功能

var myArray = [10,20,30];

$.each( myArray, function(index, value) {
   console.log('element at index ' + index + ' is ' + value);
});

//Output
element at index 0 is 10
element at index 1 is 20
element at index 2 is 30

2) Using .each()method

2) 使用.each()方法

$('#dv').children().each(function(index, element) {
    console.log('element at index ' + index + 'is ' + (this.tagName));
    console.log('current element as dom object:' + element);
    console.log('current element as jQuery object:' + $(this));
});

//Output
element at index 0 is input
element at index 1 is p
element at index 2 is span


Resources

资源

回答by tig

from http://api.jquery.com/jQuery.each:

来自http://api.jquery.com/jQuery.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.

$.each() 函数与 .each() 不同,后者用于专门遍历 jQuery 对象。$.each() 函数可用于迭代任何集合,无论是映射(JavaScript 对象)还是数组。

回答by Michael Wright

You want to really use $.eachwith an array that isn't elements or something. ie:

你想真正使用$.each一个不是元素或其他东西的数组。IE:

var x = ["test", "test2"];

You'd use $.each(x...to traverse that instead of x.each:)

你会$.each(x...用来遍历它而不是x.each:)

.eachis for elements only :)

.each仅适用于元素:)

回答by StuperUser

The first will run the callback function to the elements in the collection you've passed in, but your code is not syntactically correct at the moment for it.

第一个将对您传入的集合中的元素运行回调函数,但您的代码目前在语法上不正确。

It should be:

它应该是:

$.each($('#myTable input[name="deleteItem[]"]:checked'), do_something);

See: http://api.jquery.com/jQuery.each/

见:http: //api.jquery.com/jQuery.each/

The second will run the function on each element of the collection you are running it on.

第二个将在您运行它的集合的每个元素上运行该函数。

See: http://api.jquery.com/each/

见:http: //api.jquery.com/each/

回答by jAndy

There is no functional difference. Every jQuery object owns a .each()method inherited from jQuery.fn. By calling this object method, jQuery already knows which Array (-like object)to iterate over. In other words, it loops over the indexed propertysfrom the current jQuery object.

没有功能差异。每个 jQuery 对象都拥有一个.each()jQuery.fn. 通过调用 this object method,jQuery 已经知道Array (-like object)要迭代哪个。换句话说,它indexed propertys从当前的 jQuery 对象循环。

$.each()on the other hand is just a "helper tool" which loops over any kind of Arrayor Object, but of course you have to tell that method which target you want to iterate.
It'll also take care of you whether you pass in an Array or object, it does the right thing using a for-inor for loopunder the hood.

$.each()另一方面,它只是一个“辅助工具”,它在任何类型的Arrayor 上循环Object,但当然你必须告诉那个方法你想要迭代哪个目标。
无论您是传入数组还是对象,它都会照顾您,它使用 afor-infor loop在引擎盖下做正确的事情。

回答by Jamie Dixon

In the first case you can iterate over jQuery objects and also other array items as indicated here:

在第一种情况下,您可以遍历 jQuery 对象和其他数组项,如下所示:

jQuery.each()

jQuery.each()

In the second case you can only itterate over jQuery objects as indicated here:

在第二种情况下,您只能迭代 jQuery 对象,如下所示:

.each()

。每个()

回答by Seth

From what I understand $.each();loops through an object or array and gives you the iterator and value of each item.

据我所知,$.each();循环遍历一个对象或数组,并为您提供每个项目的迭代器和值。

$().each();loops through a list of jQuery objects and gives you the iterator and the jQuery object.

$().each();循环遍历 jQuery 对象列表,并为您提供迭代器和 jQuery 对象。

回答by Odnxe

Taken from http://api.jquery.com/jQuery.each/

取自http://api.jquery.com/jQuery.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, 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.

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