Should I use window.load or document.ready jquery

后端 未结 2 1354
梦毁少年i
梦毁少年i 2020-12-06 10:24

Recently I saw that you could use either

$(\'document\').ready(function() {
//Do Code
});

or

$(\'window\').load(function(         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 10:45

    $('document').ready runs the code when the DOM is ready, but not when the page itself has loaded, that is, the site has not been painted and content like images have not been loaded.

    $(window).load runs the code when the page has been painted and all content has been loaded. This can be helpful when you need to get the size of an image. If the image has no style or width/height, you can't get its size unless you use $(window).load.

提交回复
热议问题