当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jquery中$().each,$.each的区别讲解

jquery中$().each,$.each的区别讲解

2018年10月10日  | 移动技术网IT编程  | 我要评论

在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法。

$().each:

对于这个方法,在dom处理上面用的较多。如果页面有多个input标签类型为checkbox,对于这时用$().each来处理多个checkbook,例如:

$(“input[name=’ch’]”).each(function(i){
if($(this).attr(‘checked’)==true)
{
//一些操作代码
}
)}

$(this)代表当前遍历的对象,i就为遍历的索引。

$.each():

对于遍历一个数组,用$.each()来处理,例如:

var arr1 = [ “one”, “two”, “three”, “four”, “five” ];
$.each(arr1, function(){
alert(this);
});
输出:one   two  three  four   five
$.each([{“name”:”limeng”,”email”:”xfjylimeng”},{“name”:”hehe”,”email”:”xfjylimeng”},function(i,item)
{
alert(“索引:”+i,”对应值为:”+item.name);
});
var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
$.each(arr2, function(i, item){
alert(item[0]);
});
输出:1   4   7

item代表当前的遍历的对象,i为遍历的索引。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网