JS foreach 用法

使用范例
$(selector).each(function(index,element))


遍历json
[
{
  "text" : "王家湾",
  "value" : "9"
},
{
  "text" : "李家湾",
  "value" : "10"
},
{
  "text" : "邵家湾",
  "value" : "13"
}
]


$.each(data,function(entryIndex,entry){
     alert(entry['text'] );
       alert(entry['value'] );
    }); 

遍历数组

each处理一维数组
  var arr1 = [ "aaa", "bbb", "ccc" ];     
  $.each(arr1, function(i,val){     
      alert(i);  
      alert(val);
  });


遍历页面标签

$("img").each(function(i){
this.src = "test" + i + ".jpg";
});

其他参照Jquery 元素选择器


 

猜你喜欢

转载自417755712.iteye.com/blog/2261136
今日推荐