jquery插件在其他公共(public)函数中调用公共(public)函数

标签 jquery jquery-plugins

我根据 http://docs.jquery.com/Plugins/Authoring 定义了我的插件

(function( $ ){

  var methods = {
    init : function( options ) {  },
    show : function( options ) {  },
    hide : function( ) {  },
    update : function( content ) { 
      // How to call the show method inside the update method
      // I tried these but it does not work
      // Error: not a function
      this.show(); 
      var arguments = { param: param };
      var method = 'show';
      // Error: options is undefined
      methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));  
    }
  };

  $.fn.tooltip = function( method ) {

    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    

  };

})( jQuery );

如何在update方法中调用show方法?

编辑:

show 方法引用 this。使用 methods.show(options)methods['show'](Array.prototype.slice.call(arguments, 1 )); 可以调用 show 方法,但对 this 的引用似乎是错误的,因为我收到 this.find(...) is not a function 错误。

show 方法:

show: function(options) {
    alert("Options: " + options);
    alert("Options Type: " + options.interactionType);
    var typeCapitalized = capitalize(options.interactionType);
    var errorList = this.find('#report' + typeCapitalized);
    errorList.html('');
},

最佳答案

var methods = {
  init : function( options ) {  },
  show : function( options ) {  },
  hide : function( ) {  },
  update : function( options ) { 

    methods.show.call(this, options);

  }
};

关于jquery插件在其他公共(public)函数中调用公共(public)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5655149/

相关文章:

jquery - 可使用 jQuery 和 Ajax 进行排序

javascript - 将多个 API 调用合并为一个请求

javascript - 全日历事件的链接

javascript - 使用标签上的 jQuery .mouseenter 突出显示段落中的单词?

javascript - 修复我的 jQuery 高亮插件

javascript - 检查是否选中了带有多个复选框的 html 表上的特定复选框。 JS/JQuery

jquery-autocomplete 插件搜索

Javascript单页应用导览工具

jquery循环: return to the first slide

javascript - 位置 : fixed and absolute at the same time. 如何?