each是jquery提供的公用方法,用来迭代出所有符合同一规则的值的索引(index)和内容(value),使用如下:
1 遍历某个元素,如所有div
$("div").each(function(index,value){
console.log(“index:”+index+",value:"+$(this).attr("id"));
});
$.each($("div"),function(index,value){
console.log(index+"==="+$(this).attr("id")+"===="+$(value).text());
});
2 遍历一个数组
var obj={one:1,two:2,three:3,four:4};
$.each(obj,function(index,value){
console.log(index+"==="+value);
});
3 遍历一个对象
var obj={one:1,two:2,three:3,four:4};
$.each(obj,function(index,value){
console.log((index+"==="+value);
});
1 遍历某个元素,如所有div
$("div").each(function(index,value){
console.log(“index:”+index+",value:"+$(this).attr("id"));
});
$.each($("div"),function(index,value){
console.log(index+"==="+$(this).attr("id")+"===="+$(value).text());
});
2 遍历一个数组
var obj={one:1,two:2,three:3,four:4};
$.each(obj,function(index,value){
console.log(index+"==="+value);
});
3 遍历一个对象
var obj={one:1,two:2,three:3,four:4};
$.each(obj,function(index,value){
console.log((index+"==="+value);
});