本文介绍了jQuery - 多个$(文档).ready ...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题:
如果我链接两个JavaScript文件,都带有 $(document).ready
函数, 怎么了?有人会覆盖另一个吗?或者同时执行 $(文件).ready
调用?
If I link in two JavaScript files, both with $(document).ready
functions, what happens? Does one overwrite the other? Or do both $(document).ready
get called?
例如,
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="http://.../jquery1.js"></script>
<script type="text/javascript" src="http://.../jquery2.js"></script>
jquery1.js:
jquery1.js :
$(document).ready(function(){
$("#page-title").html("Document-ready was called!");
});
jquery2.js:
jquery2.js:
$(document).ready(function(){
$("#page-subtitle").html("Document-ready was called!");
});
我确信最好将两个电话合并到一起一个 $(文件).ready
但在我的情况下这是不可能的。
I'm sure it is best practice to simply combine both calls into a single $(document).ready
but it's not quite possible in my situation.
推荐答案
所有将被执行并且首先调用首次运行!!
All will get executed and On first Called first run basis!!
<div id="target"></div>
<script>
$(document).ready(function(){
jQuery('#target').append('target edit 1<br>');
});
$(document).ready(function(){
jQuery('#target').append('target edit 2<br>');
});
$(document).ready(function(){
jQuery('#target').append('target edit 3<br>');
});
</script>
如您所见,他们不会互相替换
Demo As you can see they do not replace each other
还有一件事我想提一下
代替这个
$(document).ready(function(){});
您可以使用此快捷方式
jQuery(function(){
//dom ready codes
});
这篇关于jQuery - 多个$(文档).ready ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!