当在同一HTML页面上使用的两个JavaScript文件中有两个jQuery $(document).ready调用时会发生什么? [英] What happens when you have two jQuery $(document).ready calls in two JavaScript files used on the same HTML page?

查看:302
本文介绍了当在同一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;
})

现在我的问题是:



  1. 功能被解雇了

  2. 如果是,由于
    文件将以相同的
    时间为两个人准备就绪,所以他们被解雇的顺序是什么? >
  3. 推荐这种方法或者我们应该理想地只有1
    $(document).ready?

  4. 所有的执行顺序是相同的浏览器(IE,FF等)?


谢谢。

解决方案



  1. 这两个ready事件功能是否会被触发?

  2. <

是的,他们都会被解雇。


< ol start =2>
  • 他们被解雇的顺序是什么,因为这两个文件将同时准备好?


  • 以他们出现的方式(从上到下),因为ready事件将被触发一次,所有的事件监听器将会一次通知另一个。



    1. 推荐这种方法或者我们应该理想地只有1 $(文档).ready?


    可以这样做。如果您可以使用相同的块代码,那么管理更容易,但这就是它的一切。更新:显然我忘了提及,如果您在多个文件中执行此操作,您将增加JavaScript代码的大小。



    1. 所有浏览器(IE,FF等)的执行顺序是否相同?


    是的,因为jQuery会在手边进行跨浏览器的归一化。


    I have a question on jQuery $(document).ready

    Let'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 follows

    Inside script1.js:

    $(document).ready(function(){
        globalVar = 1;
    })
    

    Inside script2.js:

    $(document).ready(function(){
        globalVar = 2;
    })
    

    Now my Questions are:

    1. Will both these ready event function get fired ?
    2. If yes, what will the order in which they get fired, since the document will be ready at the same time for both of them?
    3. Is this approach recommended OR we should ideally have only 1 $(document).ready ?
    4. Is the order of execution same across all the browsers (IE,FF,etc)?

    Thank you.

    解决方案

    1. Will both these ready event function get fired ?

    Yes, they will both get fired.

    1. what will the order in which they get fired, since the document will be ready at the same time for both of them?

    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.

    1. Is this approach recommended OR we should ideally have only 1 $(document).ready ?

    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.

    1. Is the order of execution same across all the browsers (IE,FF,etc)?

    Yes, because jQuery takes the cross-browser normalization at hand.

    这篇关于当在同一HTML页面上使用的两个JavaScript文件中有两个jQuery $(document).ready调用时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆