jQuery $(document).ready 不开火?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6529800/
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
$(document).ready not firing?
提问by Hanna
Such simple code, why isn't it working? When the page loads, it should display an alert box that reads "ready".
这么简单的代码,为什么不工作?当页面加载时,它应该显示一个警告框,上面写着“准备好了”。
<!DOCTYPE html>
<html>
<head>
<title>
Title
</title>
<script type="text/javascript">
$(document).ready(function() {
alert("ready");
});
</script>
</head>
<body>
Content
</body>
</html>
I feel like it's something incredibly obvious, but I'm at a point where I can't think straight.
我觉得这是非常明显的事情,但我正处于无法直接思考的地步。
I've tried both in the latest versions of Chrome and Firefox.
我已经在最新版本的 Chrome 和 Firefox 中尝试过。
回答by orkutWasNotSoBad
Where's your jquery ref?
你的 jquery 引用在哪里?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
回答by xr280xr
I had this problem, but jQuery was referenced. I had accidentally written the previous script tag as a self closing tag when moving my javascript to an external file:
我遇到了这个问题,但引用了 jQuery。将我的 javascript 移动到外部文件时,我不小心将之前的脚本标记编写为自结束标记:
<script type="text/javascript" src="/wherever/whatever.js" />
<script type="text/javascript">
$(document).ready(function(){ /* not hit */ });
</script>
The external reference tag can't be self-closing. It should read <script type="text/javascript" src="/wherever/whatever.js"></script>
外部参考标签不能自闭合。它应该读<script type="text/javascript" src="/wherever/whatever.js"></script>
回答by Alex Wayne
You forgot to include jQuery, so $
is undefined.
您忘记包含 jQuery,因此$
未定义。
One glance at the JS console in chrome was all it took to figure out. Whenever something JS wont go, your first check should always be the console to look for an error. Usually this will tell you exaxtly what's up.
只需看一眼 Chrome 中的 JS 控制台就可以搞清楚。每当 JS 无法运行时,您的第一个检查应该始终是控制台以查找错误。通常这会告诉你具体发生了什么。
回答by matchew
very simple you are trying creating a jquery object without linking to jquery.
非常简单,您正在尝试创建一个不链接到 jquery 的 jquery 对象。
option one
选项一
link to jquery use jquery 1.6.1 (currently the latest) 1.4.x is an older version.
链接到 jquery 使用 jquery 1.6.1(目前是最新的)1.4.x 是旧版本。
see here<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
看这里<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
option two
选项二
alert(); can be called without jquery, so remove the $(document).ready()
bit
警报(); 可以在没有 jquery 的情况下调用,因此删除该$(document).ready()
位
回答by user2137480
I know this is an old issue, but I just had the same problem. I thought I'd add this for other jquery newbies. I did not find this answer on Stackoverflow. In my case, I had to change from:
我知道这是一个老问题,但我遇到了同样的问题。我想我会为其他 jquery 新手添加这个。我没有在 Stackoverflow 上找到这个答案。就我而言,我不得不改变:
<script src="jquery.min.js"></script>
<script src="lodash.min.js"></script>
<script src="app.js"></script>
to:
到:
<script src="./jquery.min.js"></script>
<script src="./lodash.min.js"></script>
<script src="./app.js"></script>
Looks like jquery needs the correct relative path specified. In my case, the script tags are inside the index.html and the dir hierarchy is flat i.e
看起来 jquery 需要指定正确的相对路径。就我而言,脚本标签在 index.html 内,目录层次结构是扁平的,即
<dirname>
-- index.html
-- app.js
-- jquery.min.js
-- lodash.min.js
回答by xr280xr
I had this problem when I had the vsdoc (Visual Studio intellisense script) included at runtime. Had to hide it at runtime like this:
当我在运行时包含 vsdoc(Visual Studio 智能感知脚本)时遇到了这个问题。不得不像这样在运行时隐藏它:
@if(false){
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0-vsdoc.js" type="text/javascript"></script>
}
回答by FishBasketGordo
You aren't referencing the jQuery library anywhere, so $(document).ready
will not work.
你没有在任何地方引用 jQuery 库,所以$(document).ready
不会工作。
回答by Jasper
You have to include jquery before your jquery code...
您必须在 jquery 代码之前包含 jquery ...