jQuery 类型错误:'undefined' 不是一个函数(评估 '$(document)')

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

TypeError: 'undefined' is not a function (evaluating '$(document)')

jquerywordpress

提问by dcolumbus

  1. I'm using a WordPress site.
  2. I'm including this script in the header.
  1. 我正在使用 WordPress 网站。
  2. 我在标题中包含了这个脚本。

When the script loads, I get this error:

当脚本加载时,我收到此错误:

TypeError: 'undefined' is not a function (evaluating '$(document)')

类型错误:'undefined' 不是一个函数(评估 '$(document)')

I have no idea what is causing it or what it even means.

我不知道是什么导致了它,或者它甚至意味着什么。

In firebug, I get this:

在萤火虫中,我得到了这个:

$ is not a function

$ 不是函数

回答by El Yobo

Wordpress uses jQuery in noConflictmode by default. You need to reference it using jQueryas the variable name, not $, e.g. use

Wordpress默认在noConflict模式下使用 jQuery 。您需要使用jQuery作为变量名来引用它,而不是$,例如使用

jQuery(document);

instead of

代替

$(document);

You can easily wrap this up in a self executing function so that $refers to jQuery again (and avoids polluting the global namespace as well), e.g.

您可以轻松地将其包装在一个自执行函数中,以便$再次引用 jQuery(并避免污染全局命名空间),例如

(function ($) {
   $(document);
}(jQuery));

回答by Leno Britto

Use jQuery's noConflict. It did wonders for me

使用 jQuery 的noConflict. 它为我创造了奇迹

var example=jQuery.noConflict();
example(function(){
example('div#rift_connect').click(function(){
    example('span#resultado').text("Hello, dude!");
    });
});

That is, assuming you included jQuery on your HTML

也就是说,假设您在 HTML 中包含了 jQuery

<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

回答by Suman Biswas

Use this:

用这个:

var $ =jQuery.noConflict();

回答by Le_Dhul

I had this problem only on Chrome.

我只在 Chrome 上遇到过这个问题。

I tried adding

我尝试添加

var $ =jQuery.noConflict();

just before calling

就在打电话之前

$(document).ready(function () {

It worked.

有效。

Thanks a lot

非常感谢

回答by Gurpreet Dhanoa

Try this snippet:

试试这个片段:

jQuery(function($) {
  // Your code.
})

It worked for me, maybe it will help you too.

它对我有用,也许它也会对你有帮助。

回答by agrath

Also check for including jQuery, followed by some components/other libraries (like jQuery UI) and then accidentially including jQuery again - this will redefine jQuery and drop the component helpers (like .datepicker) off the instance.

还要检查是否包含 jQuery,然后是一些组件/其他库(如 jQuery UI),然后意外地再次包含 jQuery - 这将重新定义 jQuery 并将组件助手(如 .datepicker)从实例中删除。

回答by Bikram Shrestha

 ;(function($){
        // your code
    })(jQuery);

Place your js code inside the closure above , it should solve the problem.

将您的 js 代码放在上面的闭包中,它应该可以解决问题。

回答by JoT

I would use this

我会用这个

(function ($) {
   $(document);
}(jQuery));

回答by Rajan Lama

I have also faced such problems many time in web develoment carrier. Actually its not JS conflict, When we load html website to the browser we face no such error, but when we load these type of website through localhost we face such problem. That's because of localhost. When we load scripts through the localhost it scans the script and renders the output. But when we didn't use localhost. It just scan the output. Example, when we write php code putside the localhost and run without host we get error. But if the code is correct and is run through host we get actual output, and when we use inspect element we get the output code in HTMl format but not in PHP format this is because of rendering of the code.

我在网络开发载体中也多次遇到过这样的问题。其实这不是JS冲突,当我们将html网站加载到浏览器时我们不会遇到这样的错误,但是当我们通过localhost加载这些类型的网站时我们会遇到这样的问题。那是因为本地主机。当我们通过本地主机加载脚本时,它会扫描脚本并呈现输出。但是当我们没有使用本地主机时。它只是扫描输出。例如,当我们在本地主机上编写 php 代码并在没有主机的情况下运行时,我们会得到错误。但是如果代码是正确的并且通过主机运行,我们会得到实际的输出,当我们使用检查元素时,我们会得到 HTMl 格式的输出代码,而不是 PHP 格式,这是因为代码的呈现。

This is rendering error. So to fix these jquery code error one of the solution may be using this method.

这是渲染错误。因此,要修复这些 jquery 代码错误,解决方案之一可能是使用此方法。

jQuery(document).ready(function($){
/******** Body of Jquery Code*****/
});

What this code does is register '$' as the varible to the function by applying jquery. Else by default the .js file is only read as javascript.

这段代码的作用是通过应用 jquery 将 '$' 注册为函数的变量。否则默认情况下 .js 文件仅作为 javascript 读取。

回答by Ganesh

You can pass $ in function()

您可以在 function() 中传递 $

jQuery(document).ready(function($){

// jQuery code is in here

});

or you can replace $(document);with this jQuery(document);

或者你可以$(document);用这个替换jQuery(document);

or you can use jQuery.noConflict()

或者你可以使用 jQuery.noConflict()

var jq=jQuery.noConflict();
jq(document).ready(function(){  
  jq('selector').show();
});