jQuery each()小议


复制代码 代码如下:

var arr1 = [ "one", "two", "three", "four", "five" ];
$.each(arr1, function(){
alert(this);
});

输出:one two three four five
复制代码 代码如下:

var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
$.each(arr2, function(i, item){ 参数的名字可以自己定义,each方法会根据第一个参数(arr2)的格式而判断回调函数方法体语法的正确性
alert(item[0]);
});

输出:1 4 7
复制代码 代码如下:

var obj = { one:1, two:2, three:3, four:4, five:5 };
$.each(obj, function(key, val) {
alert(obj[key]);
});

输出:1 2 3 4 5
复制代码 代码如下:

$.each($("input:hidden"), function(){
alert(this.name);
});
$.each($("input:hidden"), function(){
alert(this.value);
});

跳出遍历
复制代码 代码如下:

$(document).ready(
function test()
{ var index=0;
var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]]
$.each(arr1, function(i,item){
if(i==1)
{
alert("点击继续下次遍历");
return ; //return false ;则跳出遍历
}
alert('索引'+i+'_'+item[0]);
});
})

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3