2009-08-06 73 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