自定义JQuery插件-如何获取/维护用户定义的选项?

自定义JQuery插件-如何获取/维护用户定义的选项?,jquery,jquery-plugins,Jquery,Jquery Plugins,我有一个简单(轻量级)的div滑块,我在不久前写过,因为我在各种不同的项目中使用它——现在是时候将它包装到自己的插件中了(冒着它不再那么轻量级的风险!) 自从我的插件cherry出现以来,我一直在阅读和复制,这很有意义(大部分),但我一直在逃避。以下是我的插件代码: (function( $ ) { //List of callable methods var methods = { init : function(options) {

我有一个简单(轻量级)的div滑块,我在不久前写过,因为我在各种不同的项目中使用它——现在是时候将它包装到自己的插件中了(冒着它不再那么轻量级的风险!)

自从我的插件cherry出现以来,我一直在阅读和复制,这很有意义(大部分),但我一直在逃避。以下是我的插件代码:

(function( $ ) {

    //List of callable methods
    var methods = {
        init : function(options) {

            var settings = $.extend({

                optionOne   : 'aaa',
                optionTwo   : 'bbb',
                optionThree : 'ccc',
                optionFour  : 'ddd'

            }, options);        

        },

        error : function(message) {
            alert(message);            
        },        

        showSettings : function() { 
            //This bit... how to show whats in 'settings' defined in init?
        }

    }

    $.fn.ccSlider = function( method ) {

        //Calling methods
        if (methods[method]) {
            //If plugin is called with a recognised method, run that.
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if (typeof method === 'object' || !method) {
            //if plugin is called without a method, run the init() function
            return methods.init.apply( this, arguments );
        } else {
            //Run the error() function to catch all else.
            return methods.error("ccSlider: Method '"+method+"' does not exist!");            
        }
    };

})( jQuery );
这里的一切都按预期工作,但我无法继续编写所需的方法,只是因为我不知道如何在init()方法之外访问“settings”的内容


我用“showSettings”作为测试。。。我如何编写一个showSettings方法来弹出并告诉我指定设置(比如optionTwo)的值是多少

你的架构是正确的,但是方法和设置对于你的插件来说都应该是全局的

(function( $ ) {
   var defaults = {
        optionOne   : 'aaa',
        optionTwo   : 'bbb',
        optionThree : 'ccc',
        optionFour  : 'ddd'
   } //if there are any

   var settings = {}; //you can reach the settings from any where in your plugin now
   //List of callable methods
   var methods = {
      init : function(options) {
         //initialize the settings in your init function and you are good to go
         settings = $.extend({},defaults,options);        
      },

      error : function(message) {
        alert(message);            
      },        

      showSettings : function() { 
        //This bit... how to show whats in 'settings' defined in init?
      }

  }

  //your plugin code here like in your post but now you can use the settings variable      we defined in your plugin
});

你的架构是正确的,但是方法和设置对于你的插件来说都应该是全局的

(function( $ ) {
   var defaults = {
        optionOne   : 'aaa',
        optionTwo   : 'bbb',
        optionThree : 'ccc',
        optionFour  : 'ddd'
   } //if there are any

   var settings = {}; //you can reach the settings from any where in your plugin now
   //List of callable methods
   var methods = {
      init : function(options) {
         //initialize the settings in your init function and you are good to go
         settings = $.extend({},defaults,options);        
      },

      error : function(message) {
        alert(message);            
      },        

      showSettings : function() { 
        //This bit... how to show whats in 'settings' defined in init?
      }

  }

  //your plugin code here like in your post but now you can use the settings variable      we defined in your plugin
});

…或最佳实践,根据JQuery网站…或最佳实践,根据JQuery网站…这就是我的想法,但我就是无法让它工作。我休息了一会儿,然后又恢复了工作,谢谢:)这就是我想的,但我就是不能让它工作。我休息了一会儿,然后又回来了,让它继续运行,谢谢:)