js 一数组分割成若干个数组,并装换成字符串赋个li标签
success: function (datas) { //请求成功后处理函数。 var htmltext = ''; var data = datas.result; console.log(data) var ihtml = []; for (var i in data) { ihtml.push('<div class="col-md-3 col-sm-6 col-xs-6">' + '<img src="' + data[i].img + '" width="240" height="180"/>' + '</div>') } var len = ihtml.length/4; len = parseInt(ihtml.length%4!=0?len+1:len); console.log(len); for(var i=0;i<len;i++){ var start = i*4; var end = start+4; end = end>ihtml.length?ihtml.length:end; ihtml.slice(start,end); console.log(ihtml.slice(start,end)) htmltext += '<li>' + '' + (ihtml.slice(start,end)).join("") + '' + '</li>' console.log(1) } console.log(ihtml) console.log(htmltext); $(".coop-ul").html(htmltext); },
<ul class="coop-ul"> <li> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/01.png" width="190"> <!-- </a> --> </div> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/02.png" width="190"> <!-- </a> --> </div> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/03.png" width="190"> <!-- </a> --> </div> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/04.png" width="190"> <!-- </a> --> </div> </li> <li class="li2"> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/05.png" width="190"> <!-- </a> --> </div> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/06.png" width="190"> <!-- </a> --> </div> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/07.png" width="190"> <!-- </a> --> </div> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/08.png" width="190"> <!-- </a> --> </div> </li> <li class="li3"> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/09.jpg" width="190"> <!-- </a> --> </div> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/10.jpg" width="190" height="120"> <!-- </a> --> </div> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/07.png" width="190"> <!-- </a> --> </div> <div class="col-md-3 col-sm-6 col-xs-6"> <!-- <a href="javascript:return false;" target="_blank"> --> <img src="img/08.png" width="190"> <!-- </a> --> </div> </li> </ul>