jQuery $(document).ready() 不触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3974675/
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
jQuery $(document).ready() not firing
提问by Darbio
Using jQuery 1.4.2 from Google hosted Code.
使用来自 Google 托管代码的 jQuery 1.4.2。
Is there a reason why the following javascript does not fire all 3 document.ready functions when the document is ready?
当文档准备好时,以下 javascript 是否有理由不触发所有 3 个 document.ready 函数?
The first $(document).ready()
function, which renders headers, and the second, which gives a 'Foo' alert box triggered, but subsequent ones in new <script>
blocks aren't triggered,
第一个$(document).ready()
函数呈现标题,第二个函数触发“Foo”警告框,但<script>
不会触发新块中的后续函数,
<script type="text/javascript">
$(document).ready(function () {
Cufon.replace('h1'); // Works without a selector engine
Cufon.replace('h2'); // Works without a selector engine
Cufon.replace('h3'); // Works without a selector engine
Cufon.now();
});
$(document).ready(function () { alert("Number Foo"); });
</script>
// html tags
<script type="text/javascript">
$(document).ready(function () { alert("Number One"); });
$(document).ready(function () { alert("Number Two"); });
</script>
These are in seperate web parts, hosted on the same page in Sharepoint2010
这些位于单独的 Web 部件中,托管在 Sharepoint2010 的同一页面上
回答by fish2000
I can think of three forensic things to try, right off:
我可以立即想到三件法医尝试:
- try it with non-google-hosted libraries.
- comment out the Cufon calls -- I believe Cufon does some crazy stuff to download additional resources, yes? That may be interfering.
- sub in
$(window).load()
for one or more of your$(document).ready()
callback defs. They have different firing criteria --$(window).load()
waits for everything to load up, allegedly -- but the substitution may be revealing.
- 尝试使用非 google 托管的库。
- 注释掉 Cufon 调用——我相信 Cufon 做了一些疯狂的事情来下载额外的资源,是吗?那可能会造成干扰。
- 子中
$(window).load()
的一个或多个您的$(document).ready()
回调DEFS。他们有不同的开火标准——$(window).load()
据说是等待所有东西都装满——但换人可能会有所启发。
Of course, console.log()
and alert()
will be your in-leu-of-debugger-breakpoint best friends in this case.
当然,console.log()
和alert()
将是您-LEU-的调试器断点在这种情况下,最好的朋友。
回答by Ben
you're missing a closing curly bracket and parenthesis in the second script tag
您在第二个脚本标记中缺少右大括号和圆括号
回答by Carlos Mu?oz
You are missing a });
in the end of the last $(document).ready
您});
在最后一个 $(document).ready 的末尾丢失了
Once you correct this it should work
一旦你纠正了这个它应该工作
EDIT:Since you say now that each script tag is in a separate web part I believe the problem itself is not in the scripts. Something else in your page is messing up your code.
编辑:既然你现在说每个脚本标签都在一个单独的 web 部件中,我相信问题本身不在脚本中。您页面中的其他内容正在弄乱您的代码。