开发者

what's the difference between the two ways of writting in jquery?

开发者 https://www.devze.com 2023-03-20 08:52 出处:网络
1,$.each(a,f); 2,$(\"a\").each(f); i know the last line meaning, which represents all <a> elements in 开发者_C百科the document. Then call the each() method then invoke the function f once for

1, $.each(a,f);

2,$("a").each(f);

i know the last line meaning, which represents all <a> elements in 开发者_C百科the document. Then call the each() method then invoke the function f once for each selected element. but i don't know what's the meaning of the first one? and what's the use of it? thank you.


Be careful, in your example a does not need to be the same type of object.

$.each(array, function) is for being used on normal arrays.

The idea behind $(selector).each(function) is the possibility to use the each "operator" on the result of a jQuery selector.

Here are the links to both ways of using each:

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

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


The first one iterates over something iterable, like an array or a jQuery object. It doesn't necessarily need to iterate over elements in the document, like the second one.


The first syntax would generally be used for iterating over a JavaScript array. You could use it with a jQuery object, but the second syntax would be more readable in that scenario.


Method 1 iterates any provided array.

Method 2 iterates over each provided selector in the DOM

0

精彩评论

暂无评论...
验证码 换一张
取 消