2013-02-16 61 views
1

我有這個代碼簡單位:只能通過jQuery.fn.myPlugin()而不是jQuery.myPlugin()訪問jQuery插件嗎?

(function($) { 
    $.fn.flyInModal = function() { 
     alert('fly in?'); 
    }; 
})(jQuery); 

我可以做jQuery.fn.flyInModal()從控制檯訪問它,但如果我嘗試jQuery.flyInModal()它只返回:

TypeError: Object function (e,t){return new v.fn.init(e,t,n)} has no method 'flyInModal' 

編輯:jQuery.prototype.flyInModal()也有效。

通讀the doc我應該可以通過這種方式訪問​​它。我錯過了什麼嗎?

回答

1

通過擴展$.fn添加了一個jQuery插件,它適用於jQuery對象。也就是,

$("#selector").flyInModal(); 

這適用於flyInModal到匹配元素的集合。

通常可用的jQuery函數是通過擴展$來定義的。也就是,

$.flyInModal(); 

就你而言,你對前者感興趣。

+0

哦,這清除了事情!謝謝,我會在幾分鐘內接受! – qwerty 2013-02-16 13:23:19