本文介绍了当在同一HTML页面上使用的两个JavaScript文件中有两个jQuery $(document).ready调用时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个关于jQuery的问题 $(document).ready 假设我们有一个HTML页面其中包含2个 JavaScript 文件 < script language =javascriptsrc =script1.js >< / script> < script language =javascriptsrc =script2.js>< / script> 现在我们在这两个脚本文件中说,我们有 $(document) 如下 Inside script1.js : $(document).ready(function(){ globalVar = 1; }) Inside script2.js : $(document).ready(function(){ globalVar = 2; }) 现在我的问题是:谢谢。解决方案 是的,他们都会被解雇。以他们出现的方式(从上到下),因为ready事件将被触发一次,所有的事件监听器将会一次通知另一个。可以这样做。如果您可以使用相同的块代码,那么管理更容易,但这就是它的一切。更新:显然我忘了提及,如果您在多个文件中执行此操作,您将增加JavaScript代码的大小。是的,因为jQuery会在手边进行跨浏览器的归一化。 I have a question on jQuery $(document).readyLet's say we have a HTML page which includes 2 JavaScript files<script language="javascript" src="script1.js" ></script><script language="javascript" src="script2.js" ></script>Now let's say in both these script files, we have $(document) as followsInside script1.js:$(document).ready(function(){ globalVar = 1;})Inside script2.js:$(document).ready(function(){ globalVar = 2;})Now my Questions are:Thank you. 解决方案 Yes, they will both get fired.In the way they appear (top to bottom), because the ready event will be fired once, and all the event listeners will get notified one after another.It is OK to do it like that. If you can have them in the same block code it would be easier to manage, but that's all there is to it. Update: Apparently I forgot to mention, you will increase the size of your JavaScript code if you do this in multiple files.Yes, because jQuery takes the cross-browser normalization at hand. 这篇关于当在同一HTML页面上使用的两个JavaScript文件中有两个jQuery $(document).ready调用时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 07:41