jQuery 推送堆栈

2022-01-22 00:00:00 stack jquery javascript

我需要一些帮助来理解 pushStack

I need some help to understand pushStack

这里我做了一个例子,使用与否的结果是一样的http://jsfiddle.net/Bz5n9/2/ 所以我想我真的不明白它的用途.

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.

谢谢!

推荐答案

您当前正在添加到集合中,将一个新集合推送到堆栈上,该集合已经是结果(您可以只返回 this.添加()).它所做的不是将 .b 元素作为新元素推送到堆栈上,而是推送带有 .b 元素 和 的新集合#2 <li> 你开始用的.

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.

相反,您应该直接传递元素(没有 .add() 调用),像这样:

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

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

你可以在这里测试一下.

相关文章