是否可以为jQuery函数创建自定义前缀或命名空间?

时间:2014-04-07 14:45:55

标签: javascript jquery

这是一个jQuery函数/插件:

$.fn.greenify = function() {
    this.css( "color", "green" );
}

$("a").greenify();

我希望有一个前缀来申请我的功能,例如:

$("a").colors.greenify();

在JavaScript函数中,我可以创建一个包含我的函数的对象:

var colors = new Object();
colors.greenify = function(el) {
    $(el).css( "color", "green" );
};

colors.greenify("a");

是否可以在jQuery上创建自定义前缀并使用此$("a").colors.greenify();之类的函数工作?

感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

您可以获得的最接近的(无需更改jquery本身)是使$.fn.colors函数返回包含所需方法的对象。

(function ($) {

    $.fn.colors = function () {
        var $this = this, ret = {
            greenify: function(){
                return $this.css("color", "green");
            }            
        };
        $this.data("colors",ret);
        return ret;
    };    

}(jQuery));

$("#demo").colors().greenify();

var colors = $("#demo").colors();
colors.greenify();

var colors = $("#demo").colors().data("colors");
colors.greenify();

http://jsfiddle.net/ftLXh/

()之后没有colors,如果没有对jquery核心进行重大更改,您将失去对$("#demo")所选元素的访问权限(您必须挂钩核心才能拥有每个jquery选择更新$.fn.colors

上的属性

答案 1 :(得分:1)

正如Cookie Monster指出的那样,你可以编写一个jquery插件来执行此操作:

(function($) {

    // here we go!
    $.colours = function(element, options) {

        // plugin's default options
        // this is private property and is  accessible only from inside the plugin
        var defaults = {
        }

        // to avoid confusions, use "plugin" to reference the 
        // current instance of the object
        var plugin = this;

        // this will hold the merged default, and user-provided options
        // plugin's properties will be available through this object like:
        // plugin.settings.propertyName from inside the plugin or
        // element.data('pluginName').settings.propertyName from outside the plugin, 
        // where "element" is the element the plugin is attached to;
        plugin.settings = {}

        var $element = $(element), // reference to the jQuery version of DOM element
             element = element;    // reference to the actual DOM element

        // the "constructor" method that gets called when the object is created
        plugin.init = function() {

            // the plugin's final properties are the merged default and 
            // user-provided options (if any)
            plugin.settings = $.extend({}, defaults, options);
        }

        // public methods
        // these methods can be called like:
        // plugin.methodName(arg1, arg2, ... argn) from inside the plugin or
        // element.data('pluginName').publicMethod(arg1, arg2, ... argn) from outside 
        // the plugin, where "element" is the element the plugin is attached to;
        plugin.greenify = function() {
            $element.css( "color", "green" );

        }


        // fire up the plugin!
        // call the "constructor" method
        plugin.init();

    }

    // add the plugin to the jQuery.fn object
    $.fn.colours = function(options) {

        // iterate through the DOM elements we are attaching the plugin to
        return this.each(function() {

            // if plugin has not already been attached to the element
            if (undefined == $(this).data('colours')) {

                // create a new instance of the plugin
                // pass the DOM element and the user-provided options as arguments
                var plugin = new $.colours(this, options);

                // in the jQuery version of the element
                // store a reference to the plugin object
                // you can later access the plugin and its methods and properties like
                // element.data('pluginName').publicMethod(arg1, arg2, ... argn) or
                // element.data('pluginName').settings.propertyName
                $(this).data('colours', plugin);

            }

        });

    }

})(jQuery);

这允许你这样调用它:

$("a").colours();
$("a").data('colours').greenify();

Fiddle

结构取自boilerplate code

答案 2 :(得分:1)

在设置插件时,您可以扩展jQuery命名空间,然后在颜色命名空间中存储对插件的引用。

$.fn.colors = function () {
    return this;
};
$.fn.greenify = $.fn.colors.greenify = function() {
    this.css( "color", "green" );
};

现在您可以致电$('a').colors().greenify();