Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery自定义插件-初始化后操作_Jquery_Jquery Plugins - Fatal编程技术网

jquery自定义插件-初始化后操作

jquery自定义插件-初始化后操作,jquery,jquery-plugins,Jquery,Jquery Plugins,在将自定义函数绑定到元素后,我很难找到如何操作它。我有一个功能 jQuery.fn.myPlugin = function(opts) { this.someFunction = function() { }; $(this).keypress(function() { // do something someFunction(); }); }; $('#some-element').myPlugin({ someOption: 'op

在将自定义函数绑定到元素后,我很难找到如何操作它。我有一个功能

jQuery.fn.myPlugin = function(opts) {
    this.someFunction = function() { };

    $(this).keypress(function() {
        // do something
        someFunction();
    });
};

$('#some-element').myPlugin({ someOption: 'option'});
我想做的是在设置插件后设置可选函数(someFunction)。因此,一些类似于

$('#some-element').myPlugin("someFunction", function() { 
    // do something
});

我知道我需要myPlugin中的更多参数,并检查它是初始调用(使用opts)还是初始化后发生了更改。但不太确定如何进行此操作。

您是否考虑过使用jqueryui小部件工厂?它支持在创建后更改选项,以及自定义方法和事件

阅读jQuery文档的页面

可以使用此插件开发模式(请参阅插件方法部分):

(function( $ ){

  var methods = {
    init : function( options ) { 
      // THIS 
    },
    show : function( ) {
      // IS
    },
    hide : function( ) { 
      // GOOD
    },
    update : function( content ) { 
      // !!! 
    }
  };

  $.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 );