Javascript 未捕获的类型错误:未定义不是加载 jquery-min.js 的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10429838/
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
Uncaught TypeError: undefined is not a function on loading jquery-min.js
提问by ghostCoder
I'm building a normal webpage which requires me to load about five CSS files and ten Javascript files.
我正在构建一个普通的网页,它需要我加载大约五个 CSS 文件和十个 Javascript 文件。
- When loading them separately in the HTML page, my webpage loads fine.
Now for production, I concatenated all the Javascript into a single file, in the order needed, and all the CSS into another file. But when I try to run the web page with the concatenated files it throws an error saying:
Uncaught TypeError: undefined is not a function
- 在 HTML 页面中分别加载它们时,我的网页加载正常。
现在为了生产,我按照需要的顺序将所有 Javascript 连接到一个文件中,并将所有 CSS 连接到另一个文件中。但是当我尝试使用连接的文件运行网页时,它会抛出一个错误:
Uncaught TypeError: undefined is not a function
On the line where jquery.min.js is being loaded in the concatenated Javascript file.
在连接的 Javascript 文件中加载 jquery.min.js 的那一行。
What can I do to mitigate this? I want to concatenate all files and minify them for production. Please help.
我能做些什么来缓解这种情况?我想连接所有文件并缩小它们以进行生产。请帮忙。
EDIT:I merged the Javascript and CSS in the order they were when they were being loaded individually and were working fine.
编辑:我将 Javascript 和 CSS 按照它们单独加载时的顺序合并,并且工作正常。
回答by Dustin
Assuming this problem still has not be resolved, a lot of individual files don't end their code with a semicolon. Most jQuery scripts end with (jQuery)
and you need to have (jQuery);
.
假设这个问题还没有解决,很多单独的文件不会以分号结束它们的代码。大多数 jQuery 脚本(jQuery)
都以(jQuery);
.
As separate files the script will load just fine but as one individual file you need the semicolons.
作为单独的文件,脚本将加载得很好,但作为一个单独的文件,您需要分号。
回答by Bilal Murtaza
You might have to re-check the order in which you are merging the files, it should be something like:
您可能需要重新检查合并文件的顺序,它应该是这样的:
- jquery.min.js
- jquery-ui.js
- any third party plugins you loading
- your custom JS
- jquery.min.js
- jquery-ui.js
- 您加载的任何第三方插件
- 你的自定义 JS
回答by Bikram Shrestha
This solution worked for me
这个解决方案对我有用
;(function($){ // your code })(jQuery);
Move your code inside the closure and use $ instead of jQuery
将您的代码移到闭包内并使用 $ 而不是 jQuery
I found the above solution in https://magento.stackexchange.com/questions/33348/uncaught-typeerror-undefined-is-not-a-function-when-using-a-jquery-plugin-in-ma
我在https://magento.stackexchange.com/questions/33348/uncaught-typeerror-undefined-is-not-a-function-when-using-a-jquery-plugin-in-ma 中找到了上述解决方案
after seraching too much
搜索太多后
回答by scw
I got the same error from having two references to different versions of jQuery.
两次引用不同版本的 jQuery 时,我遇到了同样的错误。
In my master page:
在我的母版页中:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
And also on the page:
并且在页面上:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"> </script>
回答by Ralph Lavelle
I had this problem recently with the jQuery Validation plug-in, using Squishit, also getting the js error:
我最近在使用Squishit的jQuery Validation 插件中遇到了这个问题,也出现了 js 错误:
"undefined is not a function"
“未定义不是函数”
I fixed it by changing the reference to the unminifiedjquery.validate.js file, rather than jquery.validate.min.js.
我通过更改对未缩小的jquery.validate.js 文件的引用而不是 jquery.validate.min.js来修复它。
@MvcHtmlString.Create(
@SquishIt.Framework.Bundle.JavaScript()
.Add("~/Scripts/Libraries/jquery-1.8.2.min.js")
.Add("~/Scripts/Libraries/jquery-ui-1.9.1.custom.min.js")
.Add("~/Scripts/Libraries/jquery.unobtrusive-ajax.min.js")
.Add("~/Scripts/Libraries/jquery.validate.js")
.Add("~/Scripts/Libraries/jquery.validate.unobtrusive.js")
... more files
I think that the minified version of certain files, when further compressed using Squishit, for example, might in some cases not deal with missing semi-colons and the like, as @Dustin suggests, so you might have to experiment with which files you can doubly compress, and which you just leave to Squishit or whatever you're bundling with.
我认为某些文件的缩小版本,例如,当使用 Squishit 进一步压缩时,在某些情况下可能无法处理丢失的分号等问题,正如@Dustin 所建议的那样,因此您可能必须尝试使用哪些文件可以双重压缩,您只需将其留给 Squishit 或您捆绑的任何东西。
回答by Hyman
For those out there who still couldn't fix this, I did so by changing my 'this' to '$(this)' when using jQuery.
对于那些仍然无法解决此问题的人,我通过在使用 jQuery 时将“this”更改为“$(this)”来解决此问题。
E.G:
例如:
$('.icon').click(function() {
this.fadeOut();
});
Fixed:
固定的:
$('.icon').click(function() {
$(this).fadeOut();
});
回答by trejder
I've run into the very same issue, when mistakenly named variable with the very same name, as function.
我遇到了同样的问题,当错误地将变量命名为具有相同名称的函数时。
So this:
所以这:
isLive = isLive(data);
failed, generating OP's mentioned error message.
失败,生成 OP 提到的错误消息。
Fix to this was as simple as changing above line to:
解决这个问题就像将上面的行更改为:
isItALive = isLive(data);
I don't know, how much does it helps in this situation, but I decided to put this answer for others looking for a solution for similar problems.
我不知道在这种情况下它有多大帮助,但我决定将这个答案提供给其他人寻找类似问题的解决方案。
回答by Francisco Alonso
Yes, i also I fixed it changing in the js libraries to the unminified.
是的,我也修复了它在 js 库中更改为 unminified 的问题。
For example, in the tag, change:
例如,在标签中,更改:
<script type="text/javascript" src="js/jquery.ui.core.min.js"></script>
<script type="text/javascript" src="js/jquery.ui.widget.min.js"></script>
<script type="text/javascript" src="js/jquery.ui.rcarousel.min.js"></script>
For:
为了:
<script type="text/javascript" src="js/jquery.ui.core.js"></script>
<script type="text/javascript" src="js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="js/jquery.ui.rcarousel.js"></script>
Quiting the 'min' as unminified.
退出 'min' 为 unminified。
Thanks for the idea.
谢谢你的想法。
回答by Logan Wayne
Remember: Javascript functions are CASE SENSITIVE.
请记住:Javascript 函数区分大小写。
I had a case where I'm pretty sure that my code would run smoothly. But still, got an error and I checked the Javascript consoleof Google Chrome to check what it is.
我有一个案例,我非常确定我的代码会顺利运行。但是仍然出现错误,我检查了Google Chrome的Javascript 控制台以检查它是什么。
My error line is
我的错误行是
opt.SetAttribute("value",values[a]);
And got the same error message:
并收到相同的错误消息:
Uncaught TypeError: undefined is not a function
Nothing seems wrong with the code above but it was not running. I troubleshoot for almost an hour and then compared it with my other running code. My error is that it was set to SetAttribute
, which should be setAttribute
.
上面的代码似乎没有什么问题,但它没有运行。我排查了将近一个小时,然后将其与我的其他运行代码进行了比较。我的错误是它被设置为SetAttribute
,应该是setAttribute
。
回答by CodyBugstein
In case there are any morons out there like me, I had this frustrating problem because I forgot a simple
如果有像我这样的白痴,我遇到了这个令人沮丧的问题,因为我忘记了一个简单的
new
new
keyword before instantiating a new object.
实例化新对象之前的关键字。