代码之家  ›  专栏  ›  技术社区  ›  angry kiwi

jquery插件公共函数

  •  2
  • angry kiwi  · 技术社区  · 14 年前

    这是我的插件

    (function($){
        $.fn.editor = function(options){
            var defaults = {},
            settings = $.extend({},defaults, options);
            this.each(function(){
                function save(){
                    alert('voila'); 
                }
            });
        }
    })(jQuery);
    

    我想从插件外部调用save函数。我该怎么做?

    2 回复  |  直到 14 年前
        1
  •  3
  •   angry kiwi    8 年前

    这个最适合我。

    (function($){
        $.fn.editor = function(options){
            var defaults = {},
            settings = $.extend({},defaults, options);
            this.each(function(){
                function save(){
                    alert('voila'); 
                }
                $.fn.editor.externalSave= function() {
                    save();
                }
            });
    
        }
    })(jQuery);
    

    呼叫

    $(function(){
        $('div').editor();
        $.fn.editor.externalSave();
    });
    
        2
  •  1
  •   andres descalzo    14 年前

    call method

    var save = function () {
    
       var self = this; // this is a element of each
    
    };
    
    (function($){
        $.fn.editor = function(options){
            var defaults = {},
            settings = $.extend({},defaults, options);
            this.each(function(){
               save.call(this) // you can include parameters 
            });
        }
    })(jQuery);