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

前端 未结 8 847
夕颜
夕颜 2020-12-07 12:59

What is the difference between this:

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

and this:



        
相关标签:
8条回答
  • 2020-12-07 13:57

    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.

    0 讨论(0)
  • 2020-12-07 13:59

    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.

    0 讨论(0)
提交回复
热议问题