[英]ReferenceError: Can't find variable: $
I am using jQuery. 我正在使用jQuery。 This is my coding on my main page:
这是我在主页上的编码:
<script type="text/javascript" src="script.js">
</script>
and my script.js is: 而我的script.js是:
$(document).ready(function(){
$("#title").click(function () {
alert("Works!");
});
});
My full coding can be found here: http://pastie.org/8676656 . 我的完整编码可以在这里找到: http : //pastie.org/8676656 。
Using a tool on the browser, I found an error in my javascript code: 使用浏览器上的工具,我在JavaScript代码中发现错误:
ReferenceError: Can't find variable: $
on line: 在线:
$(document).ready(function() {
Any help would be appreciated. 任何帮助,将不胜感激。
You have to import jQuery before using it: 您必须先导入jQuery,然后才能使用它:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
Notice it is using //
as protocol (not http://
or https://
), it means: if your .html file is at a http://
server, it will get jQuery from http://ajax.google...
, and if it is at a https://
server, it will get it from https://ajax.google...
. 注意,它使用
//
作为协议(不是http://
或https://
),这意味着:如果.html文件位于http://
服务器上,它将从http://ajax.google...
获取jQuery。 http://ajax.google...
,如果位于https://
服务器上,它将从https://ajax.google...
。
Note : If, while developing, you open your HTML file in your browser instead of in a server, you should specify the protocol, as in this answer , otherwise it won't work: 注意 :如果在开发过程中在浏览器中而不是在服务器中打开HTML文件,则应指定协议, 如此答案所示 ,否则将不起作用:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Also, you should, if possible, place your .js files at the bottom of the page, right before closing </body>
. 另外,如果可能的话,应该在关闭
</body>
之前,将.js文件放在页面底部。 See more in here . 在这里查看更多。
在代码之前导入jQuery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"><script>
在脚本之前包含jQuery
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js></script>
这是jquery加载问题,请在所有代码和脚本之前加载jquery。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js" ></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.