Javascript 匿名函数错误:$ 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28265321/
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
Anonymous Function Error: $ is not defined
提问by Architect
Google Developer Tools is displaying the following error when my PHP page uses the content from a javascript file (my_scripts.js):
当我的 PHP 页面使用 javascript 文件 (my_scripts.js) 中的内容时,Google Developer Tools 显示以下错误:
"Uncaught ReferenceError: $ is not defined scripts.js:1 (anonymous function)"
“未捕获的 ReferenceError:$ 未定义 scripts.js:1(匿名函数)”
Content of my_scripts.js
my_scripts.js 的内容
$('a.button').click(function(){
window.location.href = "another_page.php";
});
The page and the script work as required. Clicking the element links to the requested page, but the error is there.
页面和脚本按要求工作。单击元素链接到请求的页面,但错误在那里。
1) What causes the error? 2) Can it or should it be ignored?
1)什么原因导致错误?2)它可以还是应该被忽略?
回答by kopa
1) It looks like your problem is that jQuery haven't been loaded.
1) 看起来您的问题是尚未加载 jQuery。
Make sure you load jQuery before scripts.js.
确保在scripts.js.
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
2) Errors should never be ignored.
2) 错误不应该被忽视。
回答by Yair.R
You need to load jquery libary BEFORE! Please download the jquery.min.js at http://www.jquery.com/download/also, write like this:
您需要在之前加载 jquery 库!请在http://www.jquery.com/download/下载 jquery.min.js, 也可以这样写:
$(doucment).ready(function(){
$('a.button').on('click',function(){
window.location.href = "another_page.php";
});
});
回答by kisanme
If you have already included jQuery on your file, and still its not working you should try,
如果您已经在文件中包含了 jQuery,但仍然无法正常工作,您应该尝试,
jQuery(doucment).ready(function(){
jQuery('a.button').on('click',function(){
window.location.href = "another_page.php";
});
});
This is causing as there might be some conflicting js files.
这是因为可能存在一些冲突的 js 文件。

