//调用热门标签的列表接口 $.ajax({ url: getv1 + ‘/index/tag/hotTags‘, type: ‘GET‘, dataType: ‘json‘, data: { inscode: jxinscode }, success: function(res) { var labellen = res.data.length; if (labellen > 0) { var lalist = ""; for (var i = 0; i < labellen; i++) { var laname = res.data[i].name; //标签名称 var latimes = res.data[i].times; //标签使用次数 lalist += ‘<span class="label">‘ + laname + ‘<span>(‘ + latimes + ‘)</span></span>‘; } $(".labellist").append(lalist); //标签颜色随机显示 var labelindex = $(".label").length; var colorList = ["#9dc6eb", "#f8c471", "#b9a3ef", "#fdb1ca", "#9dc6eb", "#f8c471", "#b9a3ef", "#fdb1ca"]; for (var i = 0; i < labelindex; i++) { var bgColor = getColorByRandom(colorList); $(".label").eq(i).css("background", bgColor); } function getColorByRandom(colorList) { var colorIndex = Math.floor(Math.random() * colorList.length); var color = colorList[colorIndex]; colorList.splice(colorIndex, 1); return color; } } }, error: function(res) { } });
时间: 2024-10-28 20:42:25