javascript 为什么是 $(document).ready(); 不明确的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15239076/
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
Why is $(document).ready(); undefined?
提问by Dr.Knowitall
I thought that I was doing everything right, however I keep getting this error. $(document).ready(); // undefined in the console. I imported my jquery script.
我以为我做的一切都是正确的,但是我不断收到此错误。$(文件).ready(); // 在控制台中未定义。我导入了我的 jquery 脚本。
<script src = "//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script >
$(document).ready(function(){
$("div#chat").hide();
});
function send_file(){
}
function remove_selected(){
}
function changeToFile(){
}
function chatToProfile(){
}
function changeToChat(){
}
</script>
回答by Ryan Bigg
If you're running this file locally (which I suspect you are...), this will attempt to find the referenced file on your local system, which will not be there.
如果您在本地运行此文件(我怀疑您是...),这将尝试在您的本地系统上找到引用的文件,而该文件将不存在。
To fix this, instead use this:
要解决此问题,请使用以下命令:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
回答by Nishanth Nair
Need to add http: in your script reference. try this:
需要在您的脚本参考中添加 http: 。试试这个:
<script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
回答by Ravi Gadag
i use like this :) and those who says HTTP required. read this article for relative Protocol URL Protocol relative url
我是这样使用的 :) 和那些说需要 HTTP 的人。阅读这篇文章以获取相对协议 URL协议相对 url
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='/js/jquery-1.8.3.min.js' type='text/javascript'%3E%3C/script%3E")); //Load the Local file (if google is down for some reason)
}
</script>
回答by Bilal lilla
Script url that you are using for loading is perfect.
You can check your Url by posting "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" in browser url if you will get the javascript text file then it is working.
I test it and that url is working fine on my side.Error is some where else either there is restriction(security resons) or there is internet connection problem on the system when ever you run that web application on the system.
solution is either use minified copy in you web application and provide the relative path or use
您用于加载的脚本 url 是完美的。
您可以通过在浏览器网址中发布“ http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js”来检查您的网址,如果您将获得 javascript 文本文件,则它正在工作。
我对其进行了测试,该 url 在我这边工作正常。错误是其他一些地方,当您在系统上运行该 Web 应用程序时,存在限制(安全响应)或系统上存在 Internet 连接问题。
解决方案是在您的 Web 应用程序中使用缩小副本并提供相对路径或使用
<script type="text/javascript">
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='/js/jquery-1.4.2.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>
回答by Afghan Dev
You always can check if jQuery is already loaded in the memory and the library is ready to use:
您始终可以检查 jQuery 是否已加载到内存中并且库是否已准备好使用:
if (typeof jQuery !== 'undefined') {
//do whatever here
}