jQuery Chrome:未捕获的 ReferenceError:$ 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10658823/
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
Chrome: Uncaught ReferenceError: $ is not defined
提问by Ian Boyd
I am using jQuery in a web page. When using $
in Internet Explorer it works fine. When referencing $
in Chrome or Firefox it fails with error:
我在网页中使用 jQuery。$
在 Internet Explorer 中使用时,它工作正常。$
在 Chrome 或 Firefox 中引用 时失败并显示错误:
Uncaught ReferenceError: $ is not defined.
Screenshot:
截屏:
With my source code:
用我的源代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function divClick(sender, event)
{
// var chk = $(sender).find("input").first()[0];
var chk = jQuery(sender).find("input").first()[0];
alert("Works in ie");
}
</script>
</head>
<body>
<div onclick="divClick(this, event)">
<input type="checkbox">
</div>
</body>
</html>
Note:The browsers are being directed to a file on the local filesystem:
注意:浏览器被定向到本地文件系统上的一个文件:
Update: Tried changing it to jQuery
.
更新:尝试将其更改为jQuery
.
Update: Chrome finds the jquery file (i.e. no 404):
更新:Chrome 找到 jquery 文件(即没有 404):
采纳答案by Ian Boyd
And this question is here just to document the bug in Chrome and Firefox:
这个问题只是为了记录 Chrome 和 Firefox 中的错误:
Html File encoding IE9 Chrome
========================= ======= ======
Windows-1252 Works Works
UTF-8 (without BOM) Works Works
UTF-8 (with BOM EFBB) Works Works
UTF-16 (with LE BOM FFFE) Works Fails
UTF-16 (with BE BOM FEFF) Works Fails
Presumably Chrome (and Firefox) assume that a separate script
file has the same encoding as the html
file.
大概 Chrome(和 Firefox)假设一个单独的script
文件与文件具有相同的编码html
。
Chrome then tries to read jquery-1.7.2.js
as UTF-16, and is shocked to discover that the file is pure (Windows-1252) garbage.
然后 Chrome 尝试读取jquery-1.7.2.js
为 UTF-16,并震惊地发现该文件是纯 (Windows-1252) 垃圾文件。
回答by ShankarSangoli
Check whether the path of jquery-1.7.2.min.js
is correct. You can check in firebug if using firefox or chrome developer tool to see if the file is getting downloaded. You are mostly likely getting 404 for jQuery file due which $ is undefined on the page which is an alias to jQuery object.
检查路径jquery-1.7.2.min.js
是否正确。如果使用 firefox 或 chrome 开发人员工具,您可以检查 firebug 以查看文件是否正在下载。您很可能会为 jQuery 文件获得 404,因为 $ 在页面上未定义,这是 jQuery 对象的别名。
回答by Eric Leschinski
You can get this error with Javascript in Firefox and Chrome:
您可以在 Firefox 和 Chrome 中使用 Javascript 收到此错误:
Uncaught ReferenceError: $ is not defined.
If you included jquery like this:
如果您像这样包含 jquery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js">
</script>
And your server is configured to not allow javascript content to be downloaded and run at runtime, then you get the above error. Take a look at the whole error:
并且您的服务器配置为不允许在运行时下载和运行 javascript 内容,然后您会收到上述错误。看一下整个错误:
[blocked] The page at https://yoursite.com/blah# ran insecure content
from http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js.
/blah#:1
Uncaught ReferenceError: $ is not defined
What it means is your web server doesn't allow javascript content to be loaded on the fly. It's a security risk because someone could inject evil stuff in there for you to download, and own your servers.
这意味着您的 Web 服务器不允许即时加载 javascript 内容。这是一个安全风险,因为有人可能会在其中注入恶意内容供您下载并拥有您的服务器。
回答by nepdev
None of the above was applicable for me on Chrome 49. I had to specify the type as "application/javascript" like so:
以上都不适用于 Chrome 49 上的我。我必须将类型指定为“application/javascript”,如下所示:
<script src="lib/jquery-1.11.3.min.js" type="application/javascript"></script>
Using "text/javascript" was not working.
使用“text/javascript”不起作用。
回答by Patriotec
Instead of the onclick in the html, I assigned everything via id in the Jquery code.
我没有使用 html 中的 onclick,而是通过 Jquery 代码中的 id 分配所有内容。
I'm not sure if you want to know if the div is checked or if the checkbox is checked. I put the input check in the code and commented out code if the div is clicked on.
我不确定您是否想知道 div 是否已选中或复选框是否已选中。如果单击 div,我将输入检查放在代码中并注释掉代码。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
// $('#divid').unbind("click").click(function(){
$('#someid').unbind("click").click(function(){
if($(this).is(':checked')){
alert("checked");
}else{
alert("not checked");
}
});
});
</script>
</head>
<body>
<div id="divid"><input type="checkbox" id="someid"></div>
</body>
</html>
回答by Tania Petsouka
Try :
尝试 :
var chk = jQuery(sender).find("input").first()[0];
回答by laymanje
Your function needs to be wrapped in a dom ready:
您的函数需要包装在一个准备好的 dom 中:
$(function(){
function divClick(sender, event)
{
var chk = $(sender).find("input").first()[0];
alert("Works in ie");
}
});
That will allow the function to fire only after the page has loaded. also your scripts should be loaded just before the closing HTML tag. not all the top. that will allow the page to load the HTML and css faster because it does not have to wait for the JS to load.
这将允许该函数仅在页面加载后触发。您的脚本也应该在结束 HTML 标记之前加载。不是所有的顶部。这将允许页面更快地加载 HTML 和 css,因为它不必等待 JS 加载。
Also make sure you have the correct path to jQuery from your HTML.
还要确保您的 HTML 中具有正确的 jQuery 路径。
Last thing,
最后一件事,
Instead if having your click listener in inline HTML try moving it to the JS. It is just a cleaner more organized way to handle it.
相反,如果您的点击侦听器位于内联 HTML 中,请尝试将其移至 JS。这只是一种更干净、更有条理的处理方式。
$('.clickMe').click(function(){
//do stuff here
})