Why return this.each(function()) in jQuery plugins?

后端 未结 5 2096
感情败类
感情败类 2020-12-01 02:00

Some of the tutorials and examples I have seen for developing jQuery plugins tend to return

this.each(function () {
    //Plugin code here
});
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 02:21

    In short it allows you to take advantage of chaining, since it returns everything that has been done till now so the next .anyMethod() can act upon the changed/modified elements.



    Additionally, take a look at these links they will give you a lot of information on jQuery plugin development.

    http://www.webresourcesdepot.com/jquery-plugin-development-10-tutorials-to-get-started/
    http://www.learningjquery.com/2007/10/a-plugin-development-pattern
    http://snook.ca/archives/javascript/jquery_plugin

    And here you have a nice web based app that helps you jump start your jQuery plugins. http://starter.pixelgraphics.us/

提交回复
热议问题