what does “return this.each()” do in jQuery?

后端 未结 5 990
轮回少年
轮回少年 2021-02-08 21:25

I\'m looking at a jQuery plugin, which has a single function. After setting up the appropriate defaults though a constructor argument the function defines a couple of helper fu

5条回答
  •  一生所求
    2021-02-08 21:49

    It allows for one to call a plugin or an event on a bunch of elements and then apply that same function or event to all of them

    So if you do:

    $('.selector').myPlugin();
    

    And if, let us say, .selector contains 10 elements, all 10 elements would get whatever myPlugin does.


    The reason for returning that .each statement is because .each() returns whatever it was given and it allows you to chain multiple functions and plugins together on one jQuery element.

    For example:

    $('.selector').myPlugin().yourPlugin();
    

提交回复
热议问题