Jquery遍历数组

匿名 (未验证) 提交于 2019-12-02 21:53:52

$().each()方法:

$("ul>li>input:checked").each(function(i,el){    $(this).prop("checked",true); });

在一个列表中,循环每一项用each是不错的,索引,元素都给遍历出来。

$.each()方法:

1. 处理一维数组,代码如下:

$.each(["a","b","c"],function(i,el){      console.log(i+':'+el); });

控制台输出:0:a 1:b 2:c

2.处理二维数组,代码如下:

  $(function () {             $.each([["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]], function (i, el) {                 console.log(i+ ":" + el);                 //输出0:a,b,c  1:d,e,f  2:g,h,i   这时的i为数组下标,el相当于取这二维数组中的每一个数组                 $.each(el, function (index, itemobj) {                     console.log(index + ":" + itemobj);                 });             });             //输出0.:a,b,c  0:a 1:b 2:c  1:d,e,f  0:d 1:e 2:f  2:g,h,i 0:g 1:h 2:i         });

3.该方法处理json数组,代码如下:

 $(function () {             var json = [{ name: "小明", sex: "男" }, { name: "小糖", sex: "女" }, { name: "小孩", sex: "男"}];  //自定义一个json数组             $.each(json, function (index, obj) {                 console.log(index + ":" + obj.name+":"+obj.sex);             });         });
输出:0:小明:男 1:小糖:女 2:小孩:男


each其实很简单的,对吧。。




文章来源: Jquery遍历数组
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!