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

前端 未结 8 865
夕颜
夕颜 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: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.

提交回复
热议问题