调用嵌套函数到同一对象

纳林达机场

我正在开发一个插件,将对象添加到插件后,我想从外部调用一些事件到同一对象,我该怎么做

(function ($, undefined) {

    jQuery.fn.pluginname = function (options) {
        var set = $.extend({
            opacity: 0.5             
        }, options);

        //if(options.type)

        var cdiv = this;
        cdiv.css("opacity",set.opacity);

        function callthis()
        {
            cdiv.css("display","none");
        }
    }
})(jQuery);


jQuery("#first").pluginname({opacity:0.5});

jQuery("#second").pluginname({opacity:0.5});

// call this function later

jQuery("#first").pluginname("callthis");
TJ人群

通常的方法是让您的插件接受“方法”作为字符串参数,例如:

jQuery("#first").pluginname("callThis");

在插件内部,您可以将该请求路由到该函数。

通常,您还希望将最初使用的选项存储在闭包之外的其他位置。data对此很有用。

通常,您有一种"destroy"从元素中删除插件方法。

所以:

(function ($, undefined) {
    var methods = {
        init: function(options) {
            // Determine options
            var opts = $.extend({
                opacity: 0.5             
            }, options);

            // Remember them
            this.data("pluginname", opts);

            // Initial stuff
            this.css("opacity", opts.opacity);
        },

        callThis: function(opts) {
            // Use 'opts' if relevant
            this.css("display","none");
        },

        destroy: function(opts) {
            this.removeData("pluginame");
            this.css("display", "").css("opacity", "");
        }
    };

    jQuery.fn.pluginname = function (options) {
        var method, args;

        // Method?
        if (typeof options === "string") {
            // Yes, grab the name
            method = options;

            // And arguments (we copy the arguments, then
            // replace the first with our options)
            args = Array.prototype.slice.call(arguments, 0);

            // Get our options from setup call
            args[0] = this.data("pluginname");
            if (!args[0]) {
                // There was no setup call, do setup with defaults
                methods.init.call(this);
                args[0] = this.data("pluginname");
            }
        }
        else {
            // Not a method call, use init
            method = "init";
            args = [options];
        }

        // Do the call
        methods[method].apply(this, args);
    };
})(jQuery);

现场示例| 来源

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从对象,JavaScript中同一对象的其他函数的内部调用函数?

Javascript对象。如何创建一个在同一对象内调用函数的按钮

从Javascript的同一对象中使用setTimeout函数调用对象方法

Coffeescript:从同一对象中的函数调用数组函数

我可以从同一对象javascript中调用私有函数吗

在管道中的同一对象上调用两个不同的函数(%>%)

同一对象的Mongodb嵌套组

不能多次调用同一对象的方法PHP

获取位于同一对象中的函数的引用

如何模拟更新同一对象的函数/方法?

访问同一对象函数中的变量

在 jQuery 中的同一对象上调用另一个函数中定义的函数

从同一对象处置对象

如何在以下C ++代码中的同一对象上调用default并复制构造函数?

python如何执行调用函数的对象方法,该函数从同一对象调用另一个方法

向量插入同一对象

同一对象上的stringByAppendingString

同一对象的链接方法

有没有一种方法可以用同一行在同一对象上调用多个函数?

如何模拟在python中写入同一对象的对象函数?

将嵌套对象更改为同一对象,该对象的值是对象,其父对象为原型

从同一对象的不同实例中触发另一个实例的函数

在同一对象上多次调用Mockito.when?

在春季,JPA是否会连续调用findByProperty返回对同一对象的引用?

在没有任何引用的情况下在同一对象上调用多个方法

RhinoMock AssertWas两次在同一对象上调用

从不同的罐子中删除重复的代码来调用同一对象

在同时运行的两个函数中访问同一对象的属性

PHP函数“ overloops”数组返回同一对象的多个实例