关于jquery json 遍历数组遍历问题

jquery遍历json与数组方法总结
来源:&&&时间: 20:24:58&&&阅读数:
[导读] 在jquery中遍历数组或json数组我们使用最多的方法是each这个函数了或使用foreach,for也是可以实现的,下面我来给大家详细介绍jquery遍历json与数组实现。
代码如下复制代码
先我们来参考each() 方法
在jquery中遍历数组或json数组我们使用最多的方法是each这个函数了或使用foreach,for也是可以实现的,下面我来给大家详细介绍jquery遍历json与数组实现。
先我们来参考each() 方法,each()规定为每个匹配元素规定运行的函数,返回 false 可用于及早停止循环语法$(or).each(function(index,element))例& each处理一维数组& var arr1 = [ &aaa&, &bbb&, &ccc& ];&&&&& & $.each(arr1, function(i,val){&&&&& &&&&& alert(i);&& &&&&& alert(val);& });& alert(i)将输出0,1,2alert(val)将输出aaa,bbb,ccceach处理二维数组&  var arr2 = [['a', 'aa', 'aaa'], ['b', 'bb', 'bbb'], ['c', 'cc', 'ccc']]&&&&&   $.each(arr, function(i, item){&&&&& &&&&& alert(i);&& &&&&& alert(item);&&&&&   }); &arr2为一个二维数组,item相当于取这二维数组中的每一个数组。item[0]相对于取每一个一维数组里的第一个值&& alert(i)将输出为0,1,2,因为这二维数组含有3个数组元素alert(item)将输出为& ['a', 'aa', 'aaa'],['b', 'bb', 'bbb'],['c', 'cc', 'ccc']遍历json[&{&&&text& : &王家湾&,&&&value& : &9&&},&{&&&text& : &李家湾&,&&&value& : &10&&},&{&&&text& : &邵家湾&,&&&value& : &13&&}]代码&html xmlns=&http://www.w3.org/1999/xhtml&&&head&&meta http-equiv=&content-type& content=&text/ charset=utf-8&&&title&&/title&&script type=&text/& src=&script/jquery-1.2.6.min.js&&&/script&&script type=&text/javascript&&&$(document).ready(function(){&&$(&#letter-e .button&).click(function(){&&&$.getJSON(&e.json&,function(data){&&&&$(&#dictionary&).empty();&&&&$.each(data,function(entryIndex,entry){&&&&&var html = '&div class=&entry&&';&&&&&html += '&div class=&text&&' + entry['text'] + '&/div&';&&&&&html += '&div class=&value&&' + entry['value'] + '&/div&';&&&&&html += '&/div&';&&&&&&&&&&$('#dictionary').append(html);&&&&});&&&&&&&});&&&&&});&});&&/script&&/head&&body&&div class=&letters&&&&div class=&letter& id=&letter-e&&&&&h3&E&/h3&&&&div class=&button&&Load&/div&&&/div&&/div&&div id=&dictionary&&&/div&&/body&&/html&一个完整的测试实例大家可参考& &mce:script type=&text/javascript&&&!--& $(& function()& {& /* 通用例遍方法,可用于例遍对象和数组。 不同于例遍 jQuery 对象的 $().each() 方法,此方法可用于例遍任何对象。 回调函数拥有两个参数: &&& 第一个为对象的成员或数组的索引 &&& 第二个为对应变量或内容 如果需要退出 each 循环可使回调函数返回 false,其它返回值将被忽略。 */& /*例遍数组,同时使用元素索引和内容。 $.each( [0,1,2], function(index, content){ & alert( &Item #& + index + & its value is: & + content ); }); &var testPatterns =& &&&&& [ &&&&&&& 'yyyy', &&&&&&& 'yy', &&&&&&& 'MMMM', &&&&&&& 'MMM', &&&&&&& 'MM', &&&&&&& 'M', &&&&&&& 'dd', &&&&&&& 'd', &&&&&&& 'EEEE', &&&&&&& 'EEE', &&&&&&& 'a', &&&&&&& 'HH', &&&&&&& 'H', &&&&&&& 'hh', &&&&&&& 'h', &&&&&&& 'mm', &&&&&&& 'm', &&&&&&& 'ss', &&&&&&& 's', &&&&&&& 'S', &&&&&&& 'EEEE MMMM, d yyyy hh:mm:ss.S a', &&&&&&& 'M/d/yy HH:mm' &&&&& ]; $.each(testPatterns,function(){document.write('&div&'+this+'&/div&&br /&');}); */& & & /*遍历对象,同时使用成员名称和变量内容。 $.each( { name: &John&, lang: &JS& }, function(index, content){ & //alert( &Object Name: & + index + &,And Its Value is: & + content ); & alert( &Object Property Name Is: & + index + &,And Its Property Value is: & + content ); }); */& & /*例遍对象数组,同时使用成员变量内容。 var arr = [{ name: &John&, lang: &JS& },{ name: &Nailwl&, lang: &Jquery& },{ name: &吴磊&, lang: &Ext& }]; $.each( arr, function(index, content){ & alert( &The Man's No. is: & + index + &,And & + content.name + & is learning & + content.lang ); }); */& & }& );& // --&&/mce:script&& &title&Jquery each Demo&/title&& &/head&& & &body&& & &/body&& &/html&&
除非特别声明,PHP100新闻均为原创或投稿报道,转载请注明作者及原文链接原文地址:
延伸阅读 More
haowubai&&
haowubai&&
haowubai&&
haowubai&&
haowubai&&
视频教程 Video
网站服务:
专题合作 :
会员问题 : QQ
友情链接 : QQ 925939
网站投稿 :
@php100官方
php100官方微信
Copyright (C) 2007-, All Rights Reserved 版权所有 上海创恩信息技术有限公司. 沪ICP备号1)">1)">1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><li class='page' ng-if="(endIndex<li class='page next' ng-if="(currentPage
相关文章阅读遍历json数组 - pmandy - ITeye技术网站
博客分类:
&!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""&&html&&head&
&title&New Document &/title&
&meta name="Generator" content="EditPlus"&
&meta name="Author" content=""&
&meta name="Keywords" content=""&
&meta name="Description" content=""&
&script language="javascript"&
function e() {
var json = [{ "kl_id": "2",
"kl_title": "Test date",
"kl_content": "Test date",
"kl_type": "1",
{ "kl_id": "2",
"kl_title": "Test",
"kl_content": "Test",
"kl_type": "1",
"id": "2"}]
var str = "";
var arrary = new Array();
for (var one in json)
for (var key in json[one]) {
str += json[one][key] + ",";
arrary.push(json[one][key]);
alert(arrary.length);
for (i = 0; i & arrary. i++) {
alert(arrary[i]);
document.form.txtName.value =
&/script&&/head&&body&
&form name="form"&
&a href="#" onclick="e();"&Click&/a&
&input name="txtName" value="" type="text" size="50"&
&/form&&/body&&/html&
浏览: 74204 次
来自: 北京
public static DateTime? GetData ...
请问public static DateTime? GetDa ...
不错,下载下来试试,原来用vs2010时的那个工具和现在这个不 ...
[url][b][i][u]引用[list]
[*][img] ...
http://www.9958.pw/post/phpexce ...&&&您需要以后才能回答,未注册用户请先。百度知道搜索_java遍历json数组

我要回帖

更多关于 js遍历json对象 的文章

 

随机推荐