jQuery - What are differences between $(document).ready and $(window).load?

前端 未结 8 2191
旧巷少年郎
旧巷少年郎 2020-11-22 02:45

What are differences between

$(document).ready(function(){
 //my code here
});

and

$(window).load(function(){
  //my code h         


        
8条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 03:42

    document.ready is a jQuery event, it runs when the DOM is ready, e.g. all elements are there to be found/used, but not necessarily all the content.
    window.onload fires later (or at the same time in the worst/failing cases) when images and such are loaded. So, if you're using image dimensions for example, you often want to use this instead.

    Also read a related question:
    Difference between $(window).load() and $(document).ready() functions

提交回复
热议问题