日期:2014-05-16  浏览次数:20364 次

Jquery 遍历json和数组
01.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
02.<html xmlns="http://www.w3.org/1999/xhtml">  
03.<head>  
04.<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
05.<mce:script type="text/javascript" src="scripts/jquery-1.2.1.js" mce_src="scripts/jquery-1.2.1.js"></mce:script>  
06. 
07.<mce:script type="text/javascript"><!--  
08.$(  
09.function()  
10.{  
11./* 
12.通用例遍方法,可用于例遍对象和数组。 
13.不同于例遍 jQuery 对象的 $().each() 方法,此方法可用于例遍任何对象。 
14.回调函数拥有两个参数: 
15.    第一个为对象的成员或数组的索引 
16.    第二个为对应变量或内容 
17.如果需要退出 each 循环可使回调函数返回 false,其它返回值将被忽略。 
18.*/ 
19./*例遍数组,同时使用元素索引和内容。 
20.$.each( [0,1,2], function(index, content){ 
21.  alert( "Item #" + index + " its value is: " + content ); 
22.}); 
23.
24.var testPatterns =  
25.      [ 
26.        'yyyy', 
27.        'yy', 
28.        'MMMM', 
29.        'MMM', 
30.        'MM', 
31.        'M', 
32.        'dd', 
33.        'd', 
34.        'EEEE', 
35.        'EEE', 
36.        'a', 
37.        'HH', 
38.        'H', 
39.        'hh', 
40.        'h', 
41.        'mm', 
42.        'm', 
43.        'ss', 
44.        's', 
45.        'S', 
46.        'EEEE MMMM, d yyyy hh:mm:ss.S a', 
47.        'M/d/yy HH:mm' 
48.      ]; 
49.$.each(testPatterns,function(){document.write('<div>'+this+'</div><br />');}); 
50.*/ 
51. 
52. 
53./*遍历对象,同时使用成员名称和变量内容。 
54.$.each( { name: "John", lang: "JS" }, function(index, content){ 
55.  //alert( "Object Name: " + index + ",And Its Value is: " + content ); 
56.  alert( "Object Property Name Is: " + index + ",And Its Property Value is: " + content ); 
57.}); 
58.*/ 
59. 
60./*例遍对象数组,同时使用成员变量内容。 
61.var arr = [{ name: "John", lang: "JS" },{ name: "Nailwl", lang: "Jquery" },{ name: "吴磊", lang: "Ext" }]; 
62.$.each( arr, function(index, content){ 
63.  alert( "The Man's No. is: " + index + ",And " + content.name + " is learning " + content.lang ); 
64.}); 
65.*/ 
66. 
67.}  
68.);  
69.// --></mce:script>  
70.<title>Jquery each Demo</title>  
71.</head>  
72. 
73.<body>  
74. 
75.</body>  
76.<