ReferenceError:找不到变量:$ [英] ReferenceError: Can't find variable: $

查看:146
本文介绍了ReferenceError:找不到变量:$的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery。这是我的主页上的代码:

 < script type =text / javascriptsrc =script.js > 
< / script>

和我的script.js是:

  $(document).ready(function(){
$(#title)。click(function(){
alert(Works!) ;
});
});

我可以在这里找到完整的代码: http://pastie.org/8676656



使用浏览器上的工具,我发现一个错误我的javascript代码:

  ReferenceError:找不到变量:$ 

on line:

  $(document).ready(函数(){

任何帮助将不胜感激。



 <脚本  src =// ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js\"></script> 
< script type =text / javascriptsrc = script.js>< / script>

注意它使用 // 作为协议(而不是 http:// https:// ) ,这意味着:如果.html文件位于 http:// 服务器上,它将从 http://ajax.google ... ,如果它位于 https:// 服务器上,它会得到它从 https://ajax.google ...





注意:如果在开发过程中,在浏览器而不是服务器中打开HTML文件,则应指定协议,如

 < script src =http:/ / 1850609>此答案,否则将无法正常工作。 /ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js\"></script> 

另外,如果可能的话,您应该将.js文件放在页面底部,在关闭< / body> 之前。请参阅此处


I am using jQuery. This is my coding on my main page:

<script type="text/javascript" src="script.js"> 
</script>

and my script.js is:

$(document).ready(function(){    
     $("#title").click(function () {
        alert("Works!");
    });
});

My full coding can be found here: http://pastie.org/8676656.

Using a tool on the browser, I found an error in my javascript code:

ReferenceError: Can't find variable: $

on line:

$(document).ready(function() {

Any help would be appreciated.

解决方案

You have to import jQuery before using it:

<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....


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:

<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>. See more in here.

这篇关于ReferenceError:找不到变量:$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆