问题描述
||
是否可以编写使用以下语法对jQuery对象中的每个项目进行操作的jQuery插件:
(function ($){
$.fn.extend({
DoSomething: function(){
return this.each(function () {
//Do something to each item
});
//Run the general update
update();
}
});
})(jQuery);
但是,然后我想使用常规的“更新”操作来完成函数调用。
我将如何去做这样的事情?如果我在每次操作之后而不是在结束时进行更新,将会很慢,但是我会将这段代码放在几个地方,所以我希望不要将其分为两个调用:$(object).DoSomething().update();
解决方法
可能不是您所追求的,但是:
(function ($){
$.fn.extend({
DoSomething: function(){
this.each(function () {
//Do something to each item
});
//Run the general update
update();
return this;
}
});
})(jQuery);
将执行您的3功能,然后执行update()函数。
, 这样的事情行吗?
$.fn.extend({
doSomething: function (eachProcess,whenDone) {
var result = this.each(eachProcess);
whenDone();
return result;
}
});
因此,您将传入该函数以调用每个项目(eachProcess
)和完成(whenDone
)。