jQuery traversal method
each() method:
$.each(array/object, function processing); //$Object The
$(selector) called. each(function processing); //jquery object called
<!DOCTYPE html> <html> <head> <title>php.cn</title> <meta charset="utf-8" /> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <script> function f1(){ //① 遍歷對(duì)象 //$.each(對(duì)象,function(k對(duì)象屬性變量,v對(duì)象屬性值變量){}); var cat = {name:'kitty',age:5,climb:function(){console.log('在爬樹');}}; jQuery.each(cat,function(k,v){ console.log(k+'---'+v); }); //② 遍歷數(shù)組 //$.each(數(shù)組,function(k元素下標(biāo)變量,v元素值變量){}); var color = ['red','blue','green']; jQuery.each(color,function(m,n){ console.log(m+'---'+n); }); } window.onload = function(){ //③ 遍歷"jquery對(duì)象" //$('li').each(function(w dom對(duì)象下標(biāo)索引值,f代表具體的每個(gè)dom對(duì)象){}); $('li').each(function(w,f){ //this代表遍歷出來的每個(gè)“dom對(duì)象” //this ----> f //this ----> li對(duì)象(dom對(duì)象) console.log(w+"---"+f+"---"+this); f.style.color = "blue"; }); } </script> <style type="text/css"> div {width:300px;height:200px; background-color:pink;} </style> </head> <body> <h2>each遍歷方法</h2> <div id="apple"></div> <ul> <li>北京</li> <li>上海</li> <li>深圳</li> </ul> <input type="button" value="觸發(fā)1" onclick="f1()" /> </body> </html>