Posted in Javascript onMarch 18, 2010
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]); }); })
jQuery each()小议
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@