📜  jQuery 中代码执行的起点是什么?

📅  最后修改于: 2021-11-24 05:04:37             🧑  作者: Mango

jQuery 从$(document).ready()函数开始其代码执行,每当整个 HTML DOM 加载并完全由浏览器呈现时,该函数就会执行,因此事件处理程序可以正常工作而不会出现任何错误。这个$(document).ready()函数仅在浏览器加载整个 DOM 后加载脚本。

当我们不在脚本标签中使用$(document).ready()函数时,浏览器需要时间来准备文档。脚本中的 jQuery 可能会在事件处理程序或其他函数正在执行的某些内容或元素之前执行,因此这可能会导致网页中出现一些问题,因此每当整个 DOM 准备就绪时,总是有必要开始执行。所以我们使用$(document).ready()函数。

句法:

$(document).ready(function({....})); 

或者

$(function({....}));

$(document).ready()确保在加载 DOM 时执行它。当我们想要执行脚本以加载图像、视频和 iframe 等所有资源时,我们需要使用$( window ).on( “load”, 函数() { … })。

句法:

$( window ).on( "load", function() { ... })

例子:

HTML


  

    
    
    
     
    
    
    

  

    

Geeks For Geeks

         
        

            When compared with C++, Java codes are               generally more maintainable because Java               does not allow many things which may               lead bad/inefficient programming if used              incorrectly. For example, non-primitives               are always references in Java. So we               cannot pass large objects(like we can do               in C++) to functions, we always pass              references in Java. One more example,               since there are no pointers, bad memory               access is also not possible.          

       
       


输出:

文件准备好