如何将 jQuery 对象的堆栈复制到另一个 jQuery 对象,以便end
即使返回完全不相关的对象也可以在我的插件中使用?例子:
$(myselector)
.next() // Destructive operation
.doSomething()
.end() // Goes back to "myselector"
.doSomethingElse(); // Works fine!
$.fn.myPlugin = function() {
return $(unrelated); // No stack, can't call "end" on it
};
$(myselector)
.myPlugin() // Destructive operation
.doSomething()
.end() // Goes back to nowhere, since the stack is empty
.doSomethingElse(); // Doesn't work
我想修改$(unrelated)
对象以包含this
的堆栈,因此第二个示例可以工作。这是 jsFiddle 中的完整示例。