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

大城市里の小女人 提交于 2019-11-28 03:22:23
Phil

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:

Javascript Array ( or js object ) using $.each():

var myArray = [10,20,30];

jQuery.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

jQuery objects using .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

If you are seeking further examples+details, $.each vs .each()

Resources

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.

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 :)

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.

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/

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.

Odnxe

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!