日期:2014-05-16 浏览次数:20375 次
我们开始进入jquery的学习了,jquery的学习就不那么中规中矩了,我们来看一个和javascript有所区别的地方。
<!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>Insert title here</title>
<script type="text/javascript" src="jquery-1.10.1.js"></script>
<script type="text/javascript">
   window.onload = function()
   {
	   alert("niujiabin");
    }
	window.onload = function()
	{
		alert("bcd");
		}
</script>
</head>
<body>
</body>
</html>
结果是:
弹出niujiabinbin的提示框,但是我们改写成jquery:
<!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>Insert title here</title>
<script type="text/javascript" src="jquery-1.10.1.js"></script>
<script type="text/javascript">
   $(function()
   {
	   alert("abc");
	   });
	$(function(){
		
		alert("bcd");
		});
</script>
</head>
<body>
</body>
</html>
如果引用两个js文件的function,那么会产生覆盖问题,jquery使用闭包解决了此问题。
下面我们看一看js对象和jquery对象:
下面的代码能找出错误么?
<!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>Insert title here</title>
<script type="text/javascript" src="jquery-1.10.1.js"></script>
<script type="text/javascript">
   $(function()
   {
     var hello = document.getElementById("hello");
	 hello.css("color","red");
	});
</script>
</head>
<body>
<div id="hello">
  <ul>
    <li>niujiabin</li>
	<li class="abc">maybe</li>
	<li>gossipgo</li>
  </ul>	
</div>
</body>
</html>

也就是说我们的js对象并不能调用jquery的方法,那么怎么把js对象转换成jquery对象呢,很简单,加入$() 就可以了:
$(hello).css("color","red");

那么jquery对象怎么转换成js对象呢,我们可以把jquery对象想成一个数组,请看代码:
<!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>Insert title here</title>
<script type="text/javascript" src="jquery-1.10.1.js"></script>
<script type="text/javascript">
   $(function()
   {    
	 ($("li.abc")[0]).innerHTML = "niujiabinaaaa&q