一个方法执行完以后返回自己对象javascript
28 obj.say=function(){ 29 alert('my name is :'+this.username); 30 return this; 31 }
40 // 对象链式结构 41 $('imgid').say().eat();
方法执行完返回对象php
彻底同样,jquery只不过是对js进行了一些轻量级的封装而已css
21 <script> 22 imgobj=document.getElementById('imgid'); 23 // imgobj.onclick=function(){ 24 // this.src='b.png'; 25 // } 26 27 $(imgobj).click(function(){ 28 this.src='b.png'; 29 }); 30 </script>
在dom对象上面加上$(),$(dom)html
二者都是指向当前对象,只不过this对象是dom对象,$(this)是jquery对象,区别是dom对象指行dom的方法,jquery对象执行jquery的方法java
dom对象数组,而这个dom对象数组是一个jquery对象,因此jquery选择器选出来的东西均可以执行jquery的方法,而且加上下标能转化为dom对象jquery
23 val=$('h1')[1].outerHTML;
$('h1')[1];
$('h1').get(1);linux
23 val=$('h1').get(1).outerHTML;
dom对象数组thinkphp
22 <script> 23 arr=$('h1').get(); 24 alert(arr[0].outerHTML); 25 alert(arr[1].outerHTML); 26 </script>
length数组
size()方法和length属性框架
js中的setAttribute()和getAttribute
26 $('h1').each(function(i){ 27 $('h1').get()[i].setAttribute('num',i+1); 28 }); 29 30 $('h1').click(function(){ 31 this.innerHTML=this.getAttribute('num'); 32 });
由于jquery对象多为dom的数组,因此就是遍历
26 $('h1').each(function(i){ 27 $(this).attr({'num':i+1}); 28 });
index方法
76 idx=$(this).index('.menu li');
not方法
79 $('.info p').not($('.info p').eq(idx)).hide();
jquery就是js中的new Object生成的普通对象
不能共用
1.js对象->jquery对象
$(dom);
2.jquery对象->js对象
$('h1')[1];
$('h1').get(1);
size();
length;
get();
get(index);
each();
index();
data();
jQuery框架:
1.js对象和jquery对象的区别
2.js对象和jquery对象的转换
3.核心方法
4.选择器
5.筛选
6.属性选择器
7.文档处理
8.CSS处理
9.事件
10.效果
11.Ajax无刷新
12.工具
js对象和jquery对象的区别是什么?
jquery就是js中的new Object生成的普通对象
js对象和jquery对象的方法能不能共用?
不能共用
js对象和jquery对象能不能互换?(能)
1.js对象->jquery对象
$(dom);
2.jquery对象->js对象
$('h1')[1];
$('h1').get(1);
核心方法:
size();
length;
get();
get(index);
each();
index();
data();
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 } 10 11 select{ 12 width:100px; 13 height:150px; 14 } 15 </style> 16 <script src="jquery.js"></script> 17 </head> 18 <body> 19 <h1>aaaaaaaaaaaaaaa</h1> 20 <h1>aaaaaaaaaaaaaaa</h1> 21 <h1>aaaaaaaaaaaaaaa</h1> 22 <h1>aaaaaaaaaaaaaaa</h1> 23 </body> 24 <script> 25 function $(attr){ 26 obj=new Object(); 27 obj.username='user123'; 28 obj.say=function(){ 29 alert('my name is :'+this.username); 30 return this; 31 } 32 33 obj.eat=function(){ 34 alert('my am eating!'); 35 } 36 37 return obj; 38 } 39 40 // 对象链式结构 41 $('imgid').say().eat(); 42 </script> 43 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 } 10 11 select{ 12 width:100px; 13 height:150px; 14 } 15 </style> 16 <script src="jquery.js"></script> 17 </head> 18 <body> 19 <img src="a.png" id="imgid"> 20 </body> 21 <script> 22 imgobj=document.getElementById('imgid'); 23 // imgobj.onclick=function(){ 24 // this.src='b.png'; 25 // } 26 27 $(imgobj).click(function(){ 28 this.src='b.png'; 29 }); 30 </script> 31 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 } 10 11 select{ 12 width:100px; 13 height:150px; 14 } 15 </style> 16 <script src="jquery.js"></script> 17 </head> 18 <body> 19 <img src="a.png" id="imgid"> 20 </body> 21 <script> 22 imgobj=document.getElementById('imgid'); 23 24 $(imgobj).click(function(){ 25 this.src='b.png'; 26 // $(this).attr({'src':'b.png'}); 27 }); 28 </script> 29 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 } 10 11 select{ 12 width:100px; 13 height:150px; 14 } 15 </style> 16 <script src="jquery.js"></script> 17 </head> 18 <body> 19 <h1>aaaaaaaaaaaaaaaaaaa</h1> 20 <h1>bbbbbbbbbbbbbbbbbbb</h1> 21 </body> 22 <script> 23 val=$('h1')[1].outerHTML; 24 alert(val); 25 </script> 26 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 } 10 11 select{ 12 width:100px; 13 height:150px; 14 } 15 </style> 16 <script src="jquery.js"></script> 17 </head> 18 <body> 19 <h1>aaaaaaaaaaaaaaaaaaa</h1> 20 <h1>bbbbbbbbbbbbbbbbbbb</h1> 21 </body> 22 <script> 23 val=$('h1').get(1).outerHTML; 24 alert(val); 25 </script> 26 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 } 10 11 select{ 12 width:100px; 13 height:150px; 14 } 15 </style> 16 <script src="jquery.js"></script> 17 </head> 18 <body> 19 <h1>aaaaaaaaaaaaaaaaaaa</h1> 20 <h1>bbbbbbbbbbbbbbbbbbb</h1> 21 </body> 22 <script> 23 arr=$('h1').get(); 24 alert(arr[0].outerHTML); 25 alert(arr[1].outerHTML); 26 </script> 27 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 } 10 11 select{ 12 width:100px; 13 height:150px; 14 } 15 </style> 16 <script src="jquery.js"></script> 17 </head> 18 <body> 19 <h1>aaaaaaaaaaaaaaaaaaa</h1> 20 <h1>bbbbbbbbbbbbbbbbbbb</h1> 21 </body> 22 <script> 23 alert($('h1').size()); 24 alert($('h1').length); 25 </script> 26 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 } 10 11 select{ 12 width:100px; 13 height:150px; 14 } 15 </style> 16 <script src="jquery.js"></script> 17 </head> 18 <body> 19 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 20 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 21 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 22 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 23 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 24 </body> 25 <script> 26 $('h1').each(function(i){ 27 $('h1').get()[i].setAttribute('num',i+1); 28 }); 29 30 $('h1').click(function(){ 31 this.innerHTML=this.getAttribute('num'); 32 }); 33 </script> 34 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 } 10 11 select{ 12 width:100px; 13 height:150px; 14 } 15 </style> 16 <script src="jquery.js"></script> 17 </head> 18 <body> 19 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 20 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 21 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 22 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 23 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 24 </body> 25 <script> 26 $('h1').each(function(i){ 27 $(this).attr({'num':i+1}); 28 }); 29 30 $('h1').click(function(){ 31 $(this).html($(this).attr('num')); 32 }); 33 </script> 34 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 margin:0px; 10 padding:0px; 11 } 12 13 .menu{ 14 height:50px; 15 background: #272822; 16 line-height: 50px; 17 } 18 19 .menu ul{ 20 list-style:none; 21 } 22 23 .menu ul li{ 24 float: left; 25 color:#fff; 26 margin-left:15px; 27 line-height: 50px; 28 width:100px; 29 text-align:center; 30 } 31 32 .menu ul li:hover{ 33 cursor: pointer; 34 background: #ccc; 35 } 36 37 .info{ 38 height:200px; 39 background: #ccc; 40 overflow: hidden; 41 padding:15px; 42 } 43 44 .info p{ 45 display: none; 46 } 47 48 .info p:first-child{ 49 display: block; 50 } 51 52 .menu a:hover{ 53 background: #ccc; 54 } 55 </style> 56 <script src="jquery.js"></script> 57 </head> 58 <body> 59 <div class='menu'> 60 <ul> 61 <li>linux</li> 62 <li>php</li> 63 <li>javascript</li> 64 </ul> 65 </div> 66 67 <div class='info'> 68 <p>linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!</p> 69 70 <p>php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!</p> 71 <p>javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!</p> 72 </div> 73 </body> 74 <script> 75 $('.menu li').mouseenter(function(){ 76 idx=$(this).index('.menu li'); 77 78 $('.info p').eq(idx).show(); 79 $('.info p').not($('.info p').eq(idx)).hide(); 80 }); 81 </script> 82 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 margin:0px; 10 padding:0px; 11 } 12 13 .menu{ 14 height:50px; 15 background: #272822; 16 line-height: 50px; 17 } 18 19 .menu ul{ 20 list-style:none; 21 } 22 23 .menu ul li{ 24 float: left; 25 color:#fff; 26 margin-left:15px; 27 line-height: 50px; 28 width:100px; 29 text-align:center; 30 } 31 32 .menu ul li:hover{ 33 cursor: pointer; 34 background: #ccc; 35 } 36 37 .info{ 38 height:200px; 39 background: #ccc; 40 overflow: hidden; 41 padding:15px; 42 } 43 44 .info p{ 45 display: none; 46 } 47 48 .info p:first-child{ 49 display: block; 50 } 51 52 .menu a:hover{ 53 background: #ccc; 54 } 55 </style> 56 <script src="jquery.js"></script> 57 </head> 58 <body> 59 <div class='menu'> 60 <ul> 61 <li>linux</li> 62 <li>php</li> 63 <li>javascript</li> 64 </ul> 65 </div> 66 67 <div class='info'> 68 <p>linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!linux is very much!</p> 69 70 <p>php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!php is very much!</p> 71 <p>javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!javascript is very much!</p> 72 </div> 73 </body> 74 <script> 75 $('.menu li').eq(0).css({'background':'#ccc'}); 76 77 $('.menu li').mouseenter(function(){ 78 $(this).css({'background':'#ccc'}); 79 $('.menu li').not($(this)).css({'background':'#272822'}); 80 81 82 idx=$(this).index('.menu li'); 83 84 $('.info p').eq(idx).show(); 85 $('.info p').not($('.info p').eq(idx)).hide(); 86 }); 87 </script> 88 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <style> 7 *{ 8 font-family: 微软雅黑; 9 } 10 11 select{ 12 width:100px; 13 height:150px; 14 } 15 </style> 16 <script src="jquery.js"></script> 17 </head> 18 <body> 19 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 20 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 21 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 22 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 23 <h1>aaaaaaaaaaaaaaaaaaaaaa</h1> 24 </body> 25 <script> 26 $('h1').each(function(i){ 27 $(this).data({'num':i+1}); 28 }); 29 30 $('h1').click(function(){ 31 $(this).html($(this).data('num')); 32 }); 33 </script> 34 </html>