it says in the documentation to allways return the this object in all cases i've seen so far you return this.each() function. So are there anyother cases other than this开发者_JS百科.each that you would return
If you are trying to add a method similar to .prev()
or another such function included in jQuery, using this.map()
may be useful. To answer another question on this site, I created a jQuery plugin that does exactly that.
Likewise, one might want to return a string (or other data type) from a plugin (compare .attr()
, .css()
, and .data()
), most often from the first wrapped element when only one argument is passed to the method.
For most plugins, the main reasons to return this.each(function() { ... })
are:
- Your code is executed for every DOM element referred to within the jQuery object.
- It returns that same jQuery object to allow method chaining. Obviously, that doesn't apply for methods intended to return a new jQuery object, such as mine.
精彩评论