javascript 从全局范围调用 jQuery 中的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20668220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:00:01  来源:igfitidea点击:

Calling a function within jQuery from global scope

javascriptjqueryscope

提问by eje211

I've seen several issues about issues related to this, but none that provided a definite answer.

我已经看到了几个与此相关的问题,但没有一个提供明确的答案。

In this JavaScript:

在这个 JavaScript 中:

function global_function() {
    a_jquery_function();
}

$(function () {
    var a_jquery_function = function () {
        do_something();
    };
});

Can I do something so that the global function can call the jQuery function? Obviously, it can't be done the way I do it there, but I was wondering if it could be done using jQuery.fnor using custom events. For example:

我可以做些什么以便全局函数可以调用 jQuery 函数吗?显然,它不能按照我在那里做的方式来完成,但我想知道是否可以使用jQuery.fn或使用自定义事件来完成。例如:

function global_function() {
    $.fn.a_jquery_function();
}

$(function () {
    $.fn.a_jquery_function() = function () {
        do_something();
    };
});

or

或者

function global_function() {
    trigger_event('my_event', args);
}

$(function () {
    $(document).on('my_event', function(args) {
        do_something();
    });
});

I've tried the first one, but I ran into problems. If I know either is possible, I'll work on them more. But if someone out there knows that it's not possible to do this, I suppose I'll have to give it up.

我试过第一个,但我遇到了问题。如果我知道其中任何一个是可能的,我就会更多地研究它们。但如果有人知道不可能做到这一点,我想我将不得不放弃。

Thanks.

谢谢。

回答by Kevin B

You can attach the functions to the jQuery namespace, in a sense turning them into jQuery plugins. at that point, they will be available wherever jquery is.

您可以将函数附加到 jQuery 命名空间,在某种意义上将它们变成 jQuery 插件。届时,它们将在 jquery 所在的任何地方可用。

function global_function() {
    $.a_jquery_function();
}

$(function () {
    $.a_jquery_function = function () {
        do_something();
    };
});

回答by Ginden

If you don't want to pollute global namespace, create object and append functions to it.

如果您不想污染全局命名空间,请创建对象并向其附加函数。

var myModule = {};
$(function(){
    var foo = function(){/*code*/};
    myModule.bar = foo;
});

Then you call your function from anywhere by using myModule.bar();

然后您可以使用myModule.bar();从任何地方调用您的函数;

回答by Entoarox

instead of using var functionname=function(){}you should use window.functionname=function{}when declaring the function you want available globally. All global functions are properties of the windowobject, but the reverse is also true, all properties of the windowobject are global functions.

在声明您想要全局可用的函数时,var functionname=function(){}您应该使用而不是使用window.functionname=function{}。所有全局函数都是window对象的属性,反之亦然,window对象的所有属性都是全局函数。

回答by Aditya Anand

you can write your global jQuery function like this

您可以像这样编写全局 jQuery 函数

(function($){
  $.fn.func_name = function(var){
      write your code and functions here
  }
})(jQuery);

in your html or js file call $().func_name(var)to register.

在您的 html 或 js 文件中调用$().func_name(var)注册。