Jquery学习心得
注:有些jquery的例子,调用请修改jquery包的路径
1、jquery 的ready 方法比 传统html中<body>中的onload或是javascript的window.onload要高效
onload是等所有资源加载完毕才显示页面
ready则是等浏览器加载完dom结构就显示页面了
2、onload 与jquery的ready重要区别:
若重复对onload赋值,最后的值会把前面onload的值给覆盖
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
function one(){
alert("one");
}
function two(){
alert("two");
}
function three(){
alert("three");
}
window.onload = one ;
window.onload = two ;
window.onload = three;
</script>
</head>
<body>
<!--最后一个onload会把之前的onload的值给覆盖
-->
</body>
</html>
ready可以赋值多次,后面的值不会覆盖前面的值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
function one(){
alert("one");
}
function two(){
alert("two");
}
$(document).ready(function(){
one();
})
$(document).ready(function(){
two();
})
</script>
<!--ready里面加载多个方法,不会覆盖前面的ready中的方法
-->
</head>
<body>
</body>
</html>
3、onload的用法注意事项:
文件1-1: (正确执行)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>4-1-2</title>
</head>
<body>
<div id="panel">click me.</div>
<script type="text/javascript">
document.getElementById("panel").onclick=function(){
alert( "元素已经加载完毕 !");
}
/*正确执行*/
</script>
</body>
</html>
文件1-2(无法达到预期效果)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>4-1-2</title>
</head>
<body>
<script type="text/javascript">
document.getElementById("panel").onclick=function(){
alert( "元素已经加载完毕 !");
}
/*执行错误*/
</script>
<div id="panel">click me.</div>
</body>
</html>
总结:由上可知,getElementById()要等 相应的元素标签加载完毕才能获取.
若硬要把代码放在<head>标签也是可以的。
文件1-3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Panel</title>
<script type="text/javascript">
window.onload = function(){
document.getElementById("panel").onclick=function(){
alert( "元素已经加载完毕 !");
}
}
</script>
</head>
<body>
<div id="panel">click me.</div>
</body>
</html>
如上:Window.onloa=function(){}就保证了加载完相应的标签元素后,才执行方法,这样就能对相应元素操作,正确执行
4、Jquery获取DOM对象简洁,便捷,机制健全
如文件所示:
Demo1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>2-2</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div>test</div>
<script type="text/javascript">
document.getElementById("tt").style.color="red";
</script>
</body>
</html>
Demo1 在firefox中运行 控制台会报错
Demo2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>2-2</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div>test</div>
<script type="text/javascript">
if(document.getElementById("tt")){
document.getElementById("tt").style.color="red";
}
</script>
</body>
</html>
Demo2在firefox中运行,不会报错,因为先判断了要获取的元素是否存在,若存在才获取
Demo3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>2-2</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- 引入jQuery -->
<script src="http://www.cnblogs.com/scripts/jquery-1.3.1.js" type="text/javascript"></script>
</head>
<body>
<div>test</div>
<script type="text/javascript">
$('#tt').css("color","red");
</script>
</body>
</html>
Demo3在firefox中运行,不会报错,jquery内部机制已经实现了判断获取的对象是否存在,减少了开发人员的工作量。