Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery插件,对每个jQuery对象进行操作的函数,然后执行final语句_Javascript_Jquery_Jquery Plugins - Fatal编程技术网

Javascript jQuery插件,对每个jQuery对象进行操作的函数,然后执行final语句

Javascript jQuery插件,对每个jQuery对象进行操作的函数,然后执行final语句,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,是否可以使用以下语法编写jQuery插件,对jQuery对象中的每个项进行操作: (function ($){ $.fn.extend({ DoSomething: function(){ return this.each(function () { //Do something to each item }); //Run the general update update(); } }); })(jQuery); 但是我想用一个通用的“更新”操

是否可以使用以下语法编写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);

将执行您的
this.each()
功能,然后执行您的update()功能。

是否需要此功能

$.fn.extend({
  doSomething: function (eachProcess, whenDone) {
    var result = this.each(eachProcess);
    whenDone();
    return result;
  }
});

因此,您需要传入函数来调用每个项目(
eachProcess
)和完成(
whenDone
)。

哈哈。我想那正是我想要的,我想我差不多到了,除了我把返回语句放在哪里。谢谢实际上,事实证明,update被调用了两次,即使在
this.each()
中只有一个项,你知道为什么吗?hrmm。。。现在我很困惑,但我把它移到了一个函数中,而不仅仅是几行代码。现在只调用一次。谢谢你的帮助!奇怪。看起来这里只调用了一次update()get?所以可能DoSomething()被调用了两次?实际上,我在插件中定义了whenDone()函数,所以我可以在DoSomething中引用它,而不是传入它。