$. each (选择器)和 $(选择器) . each()之间的区别是什么

这两者的区别是什么:

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

还有这个:

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

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

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

我已经看过了 jQuery 文档,但还是不明白其中的区别。(是我的错觉,还是文档有时在内容清晰度方面有些“模糊”?)

新增信息:

很明显,我尝试用一个通用的例子来混淆人们的视听

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

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

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

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

我理解他们做了两件不同的事情,但是没有达到我可以说“我需要使用 $”的程度。在这种情况下,每个()和。每个(函数(){})在另一种情况下。

它们可以互换吗? 只有在某些情况下可以? 从来不可以?

76713 次浏览

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

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

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

.each is for elements only :)

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/

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

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

Taken from 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.

Description:

.each is 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.


Examples

1) Using $.each() function

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

$('#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

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 propertys from the current jQuery object.

$.each() on the other hand is just a "helper tool" which loops over any kind of Array or 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-in or for loop under the hood.

from 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 first case you can iterate over jQuery objects and also other array items as indicated here:

jQuery.each()

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

.each()

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

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