jQuery pushStack

2019-02-18 16:05发布

问题:

I need some help to understand pushStack

Here I've made an example where the results using it or not are the same http://jsfiddle.net/Bz5n9/2/ so I think i really didn't get what is it for.

Thanks!

回答1:

You're currently adding to the collection, pushing a new collection on the stack that's already the result (you could just return the result of this.add()). What this does is instead of pushing .b elements on the stack as a new element, it pushes a new set with .b elements and the #2 <li> you started with.

Instead, you instead should pass the elements directly (no .add() call), like this:

return this.pushStack(newCollection, "agregate", "");

You can test it out here.



回答2:

Ben does a good job of explaining it here:

http://www.bennadel.com/blog/1739-Using-PushStack-In-jQuery-Plugins-To-Create- New-Collections.htm