空选择器 - jQuery插件创建

Mac*_*Mac 5 jquery plugins jquery-plugins extend

如何创建不需要选择器的插件,例如:

$.pluginName();
Run Code Online (Sandbox Code Playgroud)

出于这个:

(function($)
{
    $.fn.pluginName = function(options) 
    {
            // options
    };

    // code

})(jQuery);
Run Code Online (Sandbox Code Playgroud)

而不是使用它(以防止其他库与之冲突$):

jQuery.pluginName = function(name, value, options) 
{
   // code
};
Run Code Online (Sandbox Code Playgroud)

因为如果我这样做:$.pluginName(),Firebug告诉我,$.pluginName()除非我添加这个,否则这不是一个函数:$.('a selector goes here').pluginName();.

use*_*716 6

将它放在全局jQuery而不是原型上.

(function($)
{
//---v----------------namespacing your function in the jQuery namespace
    $.pluginName = function(options) 
    {
            // options
    };

    // code

})(jQuery);
Run Code Online (Sandbox Code Playgroud)

请记住,this函数内部不是jQuery对象.它将涉及全局jQuery功能.