$.each() 和 $(data).each()的区别
二者都是循环的一种处理
$(data).each()主要用于DOM节点的循环,也可用于一个数据对象的循环,
$.each()主要用于对数据的处理
其实两者都可以处理DOM节点和数据对象,只是一个使用习惯问题
$(data).each()
$("#id input[type='checkbox']").each(function(index,item){console.log(index,item);})
//输出结果
0 <input class="check" name="xk" value="数学" type="checkbox">
1 <input class="check" name="xk" value="语文" type="checkbox">
2 <input class="check" name="xk" value="英语" type="checkbox">
var list1 =[{"id":1,"name":"小红","age":20},{"id":2,"name":"小明","age":22}];
var list2={"area1":"北京","area2":"上海","area3":"广州"};
$(list1).each(function(index,item){console.log("list1",index,item);});$(list2).each(function(index,item){console.log("list2",index,item);
});//输出结果
list1 0 {id: 1, name: "小红", age: 20}
list1 1 {id: 2, name: "小明", age: 22}
list2 0 {area1: "北京", area2: "上海", area3: "广州"}
$.each()
$.each($(".id input[type='checkbox']"),function(index,item){console.log(index,item);})
//输出结果
0 <input class="check" name="xk" value="数学" type="checkbox">
1 <input class="check" name="xk" value="语文" type="checkbox">
2 <input class="check" name="xk" value="英语" type="checkbox">
var list1 =[{"id":1,"name":"小红","age":20},{"id":2,"name":"小明","age":22}];
var list2={"area1":"北京","area2":"上海","area3":"广州"};
$.each(list1,function(index,item){console.log("list1",index,item);});$.each(list2,function(key,value){console.log("list2",key,value);
});//输出结果
list1 0 {id: 1, name: "小红", age: 20}
list1 1 {id: 2, name: "小明", age: 22}
list2 area1 北京
list2 area2 上海
list2 area3 广州
综上看来:
$.each() 和 $(data).each()对DOM节点的处理都是一样的
对数据对象的处理,除了数据结构不一样,导致输出结果不一样,其他都是一样的