The differences between window.onload and document.ready

function t(){
	alert("test1")
}
function b(){
	alert("test2")
}
window.onload =t ;
window.onload =b ;
  • 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.
  • 必须等到网页中所有内容加载完成后才能执行(包括图片)
  • 不能同时编写多个
  • 上述代码执行页面弹出“test2”
$(document).ready(function(){
	alert("Hello World!");
});


$(document).ready(function(){
	alert("Hello again!");
});
  • 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 content.
  • 网页中Dom元素加载完成即可执行,也许与Dom元素相关联的并未加载完成
  • 可以同时编写多个
  • 上述代码执行页面先后弹出“Hello World!”、"Hello again!"

posted on 2018-07-18 20:17  白薇苏叶折耳根  阅读(109)  评论(0)    收藏  举报

导航