jQuery的遍历方法通常被用来遍历DOM元素,用于数组和对象的是$.each()方法,它接受两个参数,分别指代属性名/数组索引和属性值/数组元素:
一、$.each()遍历对象和数组
$.each(arrTmp,function(index,value){ //arrTmp数组数据
console.log(index+": "+value)
});
$.each(objTmp,function(key,value){ //objTmp对象数据
console.log(key+": "+value)
});
二、each(function() {}遍历标签
function contrast(d, x, n, id) { //数据,id名,健名,id
//先渲染再对比
var articlelist = '<span class="active" onclick="pltapmsg(this)">全部</span>';
for(var k = 0; k < d.length; k++) {
articlelist += '<span onclick="pltapmsg(this)" datastatus="' + d[k].status + '" dataid="' + d[k].maingroup_id + '">' + d[k][n] + '</span>';
}
$('#' + x).html(articlelist);
$("#" + x).find("span").each(function() {
//循环对比id设置高亮
var sid = $(this).attr("dataid");
if(sid == id) {
$(this).parent().find('span').removeClass('active');
$(this).css('background', '#EAF5FF').css('color', ' #1890FF').css('border', '1px solid #1890FF');
}
//循环对比status
var status = $(this).attr("datastatus");
if(status == 0) {
$(this).css('display', 'none');
}
});
}