从 JavaScript 调用 jQuery 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9796000/
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-26 07:52:31  来源:igfitidea点击:

Call jQuery function from JavaScript

javascriptjquery

提问by Nolesh

I have some jQuery code:

我有一些 jQuery 代码:

$(function(){ 
function someFunc(){
    /*do something*/
};
.............   
});

And now I want call someFunc from JavaScript like below:

现在我想从 JavaScript 调用 someFunc,如下所示:

function test(){    
$.fn.someFunc();
}

But it doesn't work! I've seen an article how to call function in jquery from javascript. Is it possible?

但它不起作用!我看过一篇文章如何从 javascript 调用 jquery 中的函数。是否可以?

采纳答案by T.J. Crowder

Just defining a function within the closure (function) you pass into $()doesn't do anything to put it on the jQuery fnobject. To do that, you have to do it explicitly:

只是在您传入的闭包(函数)中定义一个函数$()并不能将它放在 jQueryfn对象上。要做到这一点,你必须明确地做到这一点:

$.fn.someFunc = someFunc;

Typically, you wouldn'tdo this within a callback on $(), you'd do it earlier. Plug-ins do this to add functionality, which should already be present priorto DOM ready.

通常,您不会在 上的回调中执行此操作$(),您会更早地执行此操作。插件这样做是为了添加功能,这些功能应该DOM 就绪之前就已经存在。

By way of example, here's a silly little plug-in that turns text green:

举例来说,这是一个将文本变为绿色的愚蠢的小插件:

(function($) {
  $.fn.green = green;
  function green() {
    return this.css("color", "green");
  }
})(jQuery);

...which you might use from DOM ready:

...您可能会在 DOM 就绪时使用它:

jQuery(function($) {

  $(".foo").green();

});

Live example| Live source

活生生的例子| 直播源

Note that the call to it is from a jQuery instance, not directly from $.fn.

请注意,对它的调用来自 jQuery 实例,而不是直接来自$.fn.

回答by bfavaretto

You function someFuncis not a "jQuery function", it is a function defined inside a (jQuery) DOMContentLoadedevent handler.

您的函数someFunc不是“jQuery 函数”,它是在 (jQuery)DOMContentLoaded事件处理程序中定义的函数。

You can't access it from outside because it is out of scope. Just declare it outside the load handler, and it will be accessible:

您无法从外部访问它,因为它超出了范围。只需在加载处理程序之外声明它,它就可以访问:

$(function(){ 
    // nothing here , but you can call someFunc() if you want
});

function someFunc(){
    /*do something*/
};

function test(){    
    someFunc(); // works!
}

回答by The Alpha

In jquery

在jQuery中

$.fn.someFunc = myFunc;
function myFunc()
{
    // Code here
}

is similar to plain javascript's

类似于普通的javascript

function myObj()
{
    // Code here
}
var obj= new myObj();
obj.prototype.someFunc=myFunc;
function myFunc()
{
    // Code here
}

It's just a way to add a new property to an object (prototype inheritance) and jquery's fn is just an alias of javascript's prototype. To call my 'sumFunc' method that I have added earlier in to jquery object using

它只是一种向对象添加新属性的方法(原型继承),而jquery 的fn 只是javascript 原型的别名。调用我之前添加到 jquery 对象中的“sumFunc”方法,使用

$.fn.someFunc = myFunc;

I can do

我可以

$('someElement').someFunc();

and this line will call taht function and do something to the 'someElement' (though I did nothing), hope it's clear enough. Look at T.J. Crowder's answer, it'll give you some more understanding.

并且这一行将调用 taht 函数并对“someElement”做一些事情(尽管我什么也没做),希望它足够清楚。看看 TJ Crowder 的回答,它会给你更多的理解。