执行以下代码,可以发现window.onload与$(document).ready()区别:
1、window.onload必须等待网页中所有内容加载完毕后(包括图片)才能执行;
$(document).ready()网页中所有DOM结构绘制完毕后就执行,可能DOM元素关联的东西并没有加载完
2、window.onload只能编写一个,示例,只会弹出("test2");
$(document).ready()可以编写多个
3、$(document).ready(function(){//..});可以简写成$(function(){//..};)
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>FirstjQuery</title>
</head>
<body>
<script type="text/javascript" src="scripts/jquery-1.4.js"></script>
<script type="text/javascript">
$(document).ready(function(){//等待DOM元素加载完毕
alert("Hello World!");//弹出一个框
});
$(document).ready(function(){
alert("Nice to meet you !");
});
$(function(){
alert("This is the first jquery tset!");
});
window.onload = function(){
alert("test1");
};
window.onload =function(){
alert("test2");
};
</script>
</body>
</html>