【问题标题】:Method for jQuery pluginjQuery插件的方法
【发布时间】:2013-01-14 21:17:56
【问题描述】:

我正在尝试为自定义复选框和单选按钮制作一个 jQuery 插件。

(function($)
{
    $.fn.checkboxRadio = function(options)
    {
        var defaults = some;
        ...

        return this.each(function()
        {
            var button = $(this);
            ...
        });
    }
})(jQuery);

$('input').checkboxRadio(options);现在可以使用了

如何在不更改当前范围的情况下添加方法 check,以便可能使用 $('input').checkboxRadio('check') 之类的东西?

如何处理自定义方法并在我的插件中获取其名称?

【问题讨论】:

  • 这个答案可能会有所帮助:stackoverflow.com/a/11339046/1428241
  • 谢谢,但该解决方案没有方法。
  • 另外 - 我想保持当前的代码风格,不使用$.extend
  • 您可以在插件中编写一个开关,并提供从默认值中选择用户的机会。

标签: javascript jquery html checkbox


【解决方案1】:

这里是官方jquery plugin guide

关于包装函数的部分可以找到here ("Plugin Methods")(这个例子是一个可能的工具提示插件):

(function( $ ){
  var methods = {
    init : function(options) { ... },
    show : function() { ... },
    hide : function() { ... },
    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);

[更新]解释指南中的methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )) 行:

如果您从 javascript 代码中调用 $(selector).tooltip('update', 'hello'),您希望最终调用 update 方法,将 'hello' 作为 content 参数传递,this 设置为 $(selector) 持续时间为来电。

这就是这条线所关心的:

  • 如果method == 'update'methods[method]update方法,
  • arguments 将等于 ['update', 'hello'],您必须删除第一个元素才能获取要传递给方法的参数;这正是Array.prototype.slice.call(arguments, 1) 所做的,
  • myFunc.apply(obj, argsArray) 调用函数myFunc,将argsArray 作为参数传递,并在调用期间将this 设置为obj

因此,在您的方法中,您可以调用 this.each(...) 来遍历所有选择器的项目,例如:

update: function(content) {
  this.each(function(){ $(this).data('tooltip.content', content); });
  return this;
}

【讨论】:

  • 我已经看到了。 options var 和 each 呢?
  • 不确定这段代码在做什么methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ))
  • 如果没有方法show/hide/update,所有逻辑都去哪里了(.each 等)?似乎将使用 init 代替 - 我如何在其中使用 each 来处理目标对象的每个输入?
  • 所以这个解决方案的问题是,我如何在当前方法中获得this$.fn.tooltip
【解决方案2】:

你可以像这样连接插件方法:

(function($) {
    $.fn.checkboxRadio = function(options) {
        var defaults = {
            check: 'check'
    }

        return this.each(function() {
            var o = options;
            var _this = $(this);

            if( o.check === 'check' ) {
                 _this.attr('checked','checked');
            }else if ( o.check === 'uncheck' ) {
                 _this.removeAttr('checked');
            }
        });
    }
})(jQuery);

用户文档应该是你想要的:$('input').checkboxRadio({check:'check'});

【讨论】:

  • 这似乎不是一个正确的解决方案,因为启动选项将被覆盖。要使用它,我必须提供所有其他选项以及“检查:检查”。
  • @Steve 我试图解释这个的逻辑,你可以编辑任何你喜欢的值/函数。
  • 有一种方法可以读取check,如果存在,请在不调用其他选项的情况下执行某些操作。但是,这不是我希望在我的插件中看到的解决方案,无论如何,谢谢。
猜你喜欢
  • 2011-11-24
  • 1970-01-01
  • 2012-06-21
  • 2023-03-19
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
相关资源
最近更新 更多