【问题标题】:jQuery Plugin - passing a function / method as an optionjQuery Plugin - 将函数/方法作为选项传递
【发布时间】:2011-06-30 15:53:08
【问题描述】:

这里是菜鸟问题,我的基本设置如下:

(function($) {
    $.fn.imageSlider = function(options) {
        var defaults = {
            columns: 3,
            imgHeight: 50,
            imgWidth: 75,
            jsonScript: '',
            jsonData: {},
            onComplete: $.noop  // I want a user supplied method here
        };

        var options = $.extend(defaults, options);

        // Exec plugin here

        if($.isFunction(options.onComplete)) {
            // call user provided method
            options.onComplete.call();
        }
})(jQuery)

插件调用

 $('#gallery').imageSlider({
    columns: 6,
    jsonScript: './scripts/galley.php?id=1'
 });

插件工作正常,但永远不会调用用户提供的 onComplete 函数

我需要做什么才能让 onComplete 方法工作?

【问题讨论】:

    标签: jquery plugins jquery-plugins


    【解决方案1】:

    我可以立即看到的一个问题是您没有正确关闭函数定义。

    (function($) {
      $.fn.imageSlider = function(options) {
    
        // ...setup options and code...
    
      }; // <-- missing this
    })(jQuery);
    

    除此之外,一切看起来都运行良好:http://jsfiddle.net/y4rkS/

    【讨论】:

    • 谢谢,害死我的总是小事
    猜你喜欢
    • 1970-01-01
    • 2018-01-23
    • 2013-09-11
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多