1、选择器+遍历
$('div').each(function (i){
i就是索引值
this 表示获取遍历每一个dom对象
});
2、选择器+遍历
$('div').each(function (index,domEle){
index就是索引值
domEle 表示获取遍历每一个dom对象
});
3、更适用的遍历方法
1)先获取某个集合对象
2)遍历集合对象的每一个元素
var d=$("div");
$.each(d,function (index,domEle){
d是要遍历的集合
index就是索引值
domEle 表示获取遍历每一个dom对
});
遍历一个集合例子:
$(function () {
ajaxGetData();
});
function ajaxGetData() {
var leftFlow = Array();
$.ajax({
url: "url",
type: "post",
data:{
param1:value1,
param2:value2
},
dataType: "json",
success: function (json) {
if (json.length > 0) {
$.each(json, function (i, item) {
var html = '<div style="margin-top: 40px;">' + item.name1+ '(' + item.name2+ 'MB)</div>';
leftFlow.push(html);
});
$('#id').html("");
$('#id').html(leftFlow.join(""));
}
}
});
}
后台返回的数据:
List<Map<String, Object>> mapList = this.Service.getData(User);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=UTF8");
PrintWriter writer = response.getWriter();
writer.write(JSON.toJSONString(mapList));
writer.close();