javascript 未捕获的 ReferenceError: $ 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12419724/
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 ReferenceError: $ is not defined
提问by user1670671
I've been getting undefined error and I dont know how to fix it.
我一直收到未定义的错误,我不知道如何修复它。
Here's my code:
这是我的代码:
<script type="text/javascript">
function returnBlurayDisc(member_id){
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("popup_container").innerHTML=xmlhttp.responseText;
$("#GrayBackground").css({'height':'1900px','display':'inline'});
}
}
xmlhttp.open("GET","ajax/returnAjax.php?member_id="+member_id+"&name="+name);
xmlhttp.send();
}
</script>
The error is Uncaught ReferenceError: $ is not defined. Kindly help me.
错误是 Uncaught ReferenceError: $ is not defined。请帮助我。
回答by Ben Zotto
This line:
这一行:
$("#GrayBackground").css({'height':'1900px','display':'inline'});
uses jQuery (via the $
function), which is a library you need to include in your page if you want this line of code in there.
使用 jQuery(通过$
函数),如果你想在页面中包含这行代码,它是一个你需要包含在页面中的库。
Put this in the top of your page to test:
将其放在页面顶部进行测试:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
Worth noting that if you want to adopt jQuery-- which is a fine idea in many cases-- you can use it to simplify a bunch of stuff, including the AJAX request, which you're now doing manually.
值得注意的是,如果您想采用 jQuery(在许多情况下这是一个好主意),您可以使用它来简化一堆东西,包括您现在手动执行的 AJAX 请求。
回答by Starx
$
in your code most probably refers to jQuery library. So, make sure you have included jQuery library file in your document.
$
在您的代码中很可能是指 jQuery 库。因此,请确保您已在文档中包含 jQuery 库文件。
If you use CDN then you have to include a similar tag like below on head
section of your document.
如果您使用 CDN,那么您必须在head
文档的部分包含如下类似的标签。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
This includes the JQuery Library on your document and you can finallyuse the $
to target elements.
这包括文档中的 JQuery 库,您最终可以使用$
来定位元素。