首页 > 代码库 > $(document).ready vs. $(window).load
$(document).ready vs. $(window).load
jQuery offers two powerful methods to execute code and attach event handlers: $(document).ready and $(window).load. The document ready event executes already when the HTML-Document is loaded and the DOM is ready, even if all the graphics haven’t loaded yet. If you want to hook up your events for certain elements before the window loads, then $(document).ready is the right place.
1 2 3 4 | $(document).ready( function () { // executes when HTML-Document is loaded and DOM is ready alert( "document is ready" ); }); |
The window load event executes a bit later when the complete page is fully loaded, including all frames, objects and images. Therefore functions which concern images or other page contents should be placed in the load event for the window or the content tag itself.
1 2 3 4 | $(window).load( function () { // executes when complete page is fully loaded, including all frames, objects and images alert( "window is loaded" ); }); |
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。