jQuery插件在其他公共函数内部调用公共函数 [英] jquery plugin call public function inside other public function

查看:90
本文介绍了jQuery插件在其他公共函数内部调用公共函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基于 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方法?

How to call the show method inside the update method?

编辑:

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

The show method references this. Using methods.show(options) or methods['show'](Array.prototype.slice.call( arguments, 1 )); works to call the show method but then the reference to this seems to be wrong because I got a this.find(...) is not a function error.

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插件在其他公共函数内部调用公共函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆