2009-08-06 75 views
1

为了清理我的代码,我想在我的实际jQuery插件中使用子插件,但实际上没有任何事情发生。 THX提前在jQuery插件中调用其他插件

作为一个简单的例子,请看看下面的代码:

(function($){ 
    $.fn.funct = function() { 
     // so far it seems to run the code... 
     console.log('funct is running...'); 

     return this.each(function(){ 
      // ...but nothing is happening here 
      console.log('this.each is running...'); 
      $(this).css('background', 'blue'); 
     } 
    } 
    $.fn.foo = function() { 
     return this.each(function(){ 
      console.log('plugin is running...'); 
      $(this).funct(); 
     }); 
    }; 
})(jQuery); 

回答

1

在最初的一瞥,它看起来像你没有正确关闭第一回。

$(this).css('background', 'blue'); 
     } 

应该是:

$(this).css('background', 'blue'); 
     }); 
+0

哇,好赶上! – 2009-08-06 13:26:57

0

我宁愿在一个插件火一个自定义事件,让其他插件订阅该事件。你没有依赖。

见我的答案here更多信息有关自定义事件和绑定/触发

+0

THX的小费 – 2009-08-06 13:31:05