调用嵌套函数到同一对象 [英] Call nested function to same object

查看:106
本文介绍了调用嵌套函数到同一对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing a plugin, after add object to the plugin i want to call some events to same object from outside, how can i do that

(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");

推荐答案

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

The usual way is to have your plugin accept "methods" as string arguments, e.g.:

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

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

Internally in the plugin, you route that request to the function.

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

Normally you'd also want to store the options used initially somewhere other than just in the closure; data is useful for that.

通常,您可以使用"destroy"方法从元素中删除插件.

And you normally have a "destroy" method to remove the plugin from elements.

所以:

(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);

在线示例 |

这篇关于调用嵌套函数到同一对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆