【问题标题】:Passing callback function to a jQuery plugin将回调函数传递给 jQuery 插件
【发布时间】:2018-04-07 21:27:18
【问题描述】:

我正在创建一个接受输入选项的 jQuery 插件,如 this link 中所述。以下是上述链接中的相同代码

(function ( $ ) {
    $.fn.greenify = function( options ) {
        // This is the easiest way to have default options.
        var settings = $.extend({
            // These are the defaults.
            color: "#556b2f",
            backgroundColor: "white"
        }, options );

        // Greenify the collection based on the settings variable.
        return this.css({
            color: settings.color,
            backgroundColor: settings.backgroundColor
        });
    };
}( jQuery ));

我还对插件进行了一些扩展,以便在用户传递回调函数时触发用户函数。于是,插件演变成如下:

(function ( $ ) {
    $.fn.greenify = function( options ) {
        // This is the easiest way to have default options.
        var settings = $.extend({
            // These are the defaults.
            color: "#556b2f",
            backgroundColor: "white",

            onGreenify: function () {} // This is a new option
        }, options );

        // Greenify the collection based on the settings variable.
        var newCss = this.css({
            color: settings.color,
            backgroundColor: settings.backgroundColor
        });

        // Execute the function
        if (typeof settings.onGreenify == "function") {
            settings.onGreenify ();
        }

        return newCss;
    };
}( jQuery ));

当用户这样做时:

var greenifyOptions = {
    onGreenify: function () {
        console.log("Element is now green.");
    }
}

$("#id1").greenify(greenifyOptions);

那么上面的代码将成功运行并显示消息 Element is now green. 到控制台。

到目前为止一切顺利。

现在,这是我面临的问题。如何将输入参数传递给onGreenify 函数?例如,我想做以下事情:

var name = "John";
var greenifyOptions = {
    onGreenify: function (name) {
        console.log ("Hello " + name +  ". Element is now green.");
    }
}

$("#id1").greenify(greenifyOptions);

最终应该会显示 Hello John。元素现在是绿色的。 到控制台。

但我不确定如何修改以下代码以使其接受输入参数

if (typeof settings.onGreenify == "function") {
    settings.onGreenify ();
}

请注意,此函数应接受任意数量的输入参数。我无法控制用户需要传递的内容。

我的方法对吗?需要修改的代码是什么?

谢谢。

【问题讨论】:

  • 参数应该从哪里来?该插件应该如何知道它应该传递参数或它们的值应该是什么?
  • 传递的名称必须来自插件范围,而不是全局范围,回顾这个​​概念考虑你需要,反正我放了一个演示,因为我想你尝试做的概念
  • @Pointy - 你到底是什么意思?我提供的示例中指出了这些参数
  • 想想函数调用是如何工作的。调用环境必须对它应该传递给函数的内容有所了解。您的函数被编写为期望传递一个字符串 (name)。其他一些函数可能需要一个数字列表。
  • @Pointy - 这正是我的问题。我该怎么做?

标签: javascript jquery function callback parameter-passing


【解决方案1】:

嗨,这里是一个简单的demo,但你的想法有些不好,如果名称是全局的,则无需直接传递 get .. 我在想如何通过元素传递名称来完成, 是由 de pluging 限定的东西 :

     (function ( $ ) {

    $.fn.greenify = function( options ) {

        // This is the easiest way to have default options.
        var settings = $.extend({
            // These are the defaults.
            color: "#556b2f",
            backgroundColor: "white",

            onGreenify: function () {} // This is a new option

        }, options );

        // Greenify the collection based on the settings variable.
        var newCss = this.css({
            color: settings.color,
            backgroundColor: settings.backgroundColor
        });

        // Execute the function
        if (typeof settings.onGreenify == "function") {

            //settings.onGreenify ();
            settings.onGreenify.call(this, settings.name);
        }

        return newCss;

    };

}( jQuery ));
var name = 'juan'
var greenifyOptions = {
        name: 'juaanddd',
    onGreenify: function (name) {
        console.log("Element is "+name+" now green.");
    }
}

$("#id1").greenify(greenifyOptions);

【讨论】:

  • 我对此响应进行了更新,为自定义对象或对象集提供了自定义名称
  • 谢谢。但是是否可以在不将输入参数作为选项的一部分传递的情况下做到这一点(即,在greenifyOptions 中没有name 选项。我希望用户传递函数并且它的输入参数是函数本身的一部分,如下onGreenify: function (name) ( // code here )
  • @Greeso 和名称在哪里定义?,需要在任何地方定义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多