jQuery插件公共方法/函数 [英] jQuery plugin public method/function

查看:222
本文介绍了jQuery插件公共方法/函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以下目标,但不知道出了什么问题:

I am trying to achieve something like the following but dont know whats wrong:

$.a = function() {

// some logic here

function abc(id) {
   alert('test'+id);
}


}

$.a.abc('1');

我尝试使用return函数,但这似乎也不起作用.有人可以帮忙吗.

I tried using the return function, but that doesnt seem to work either. Can someone please help.

谢谢您的时间.

推荐答案

由于$.a本身必须是一个函数,因此必须将abc函数作为属性添加到$.a函数中:

Since $.a must be a function in itself, you'll have to add the abc function as a property to the $.a function:

$.a = function () {
    // some logic here...
};

$.a.abc = function (id) {
    alert('test' + id);
};

如果必须从$.a函数中定义abc,则可以执行以下操作.请注意,使用此方法时,只有在调用$.a之前,$.a.abc才可用!在调用函数之前,不会评估函数内部的任何内容.

If abc must be defined from within the $.a function, you can do the following. Do note that $.a.abc will not be available until $.a has been called when using this method! Nothing inside a function is evaluated until a function is called.

$.a = function () {

    // Do some logic here...

    // Add abc as a property to the currently calling function ($.a)
    arguments.callee.abc = function (id) {
        alert('test' + id);
    };
};

$.a();
$.a.abc('1');

这篇关于jQuery插件公共方法/函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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