var data = [1,2,3,4,5]
function messageTpl(messageData) {
var user = messageData.user;
var content = messageData.content;
var htmlText = [];
htmlText.push('<div class="moments-item" data-index="'+ 0 +'">');
// 消息用户头像
htmlText.push('<a class="item-left" href="#">');
htmlText.push('<img src="' + user.avatar + '" width="42" height="42" alt=""/>');
htmlText.push('</a>');
htmlText.push('<div class="item-right">');
htmlText.push('<a href="#" class="item-name">' + user.name + '</a>');
htmlText.push('<p class="item-msg">' + content.text + '</p>');
var contentHtml = '';
switch(content.type) {
case 0:
contentHtml = multiplePicTpl(content.pics);
break;
case 1:
contentHtml = multiplePicshare(content.share);
break;
case 2:
contentHtml = multiplePicTpl(content.pics);
break;
case 3:
break;
}
htmlText.push(contentHtml);
htmlText.push('<div class="item-ft">');
htmlText.push('<span class="item-time">' + content.timeString + '</span>');
htmlText.push('<div class="item-reply-btn">');
htmlText.push('<span class="item-reply"></span>');
htmlText.push('<div class="comment-thumbs-up"><div class="commente"><i class="iconfont icon-buoumaotubiao15"></i>点赞</div><div class="thumbs-up"><i class="iconfont icon-pinglun1"></i>评论</div></div>');
htmlText.push('</div></div>');
htmlText.push(replyTpl(messageData.reply));
htmlText.push('</div></div>');
return htmlText.join('');
}
function render() {
var messageHtml = [];
for ( var i = 0; i < data.length; i ++){
messageHtml.push(messageTpl(data[i]));
}
messageHtml = messageHtml.join('');
$momentsList.html(messageHtml);
}
$(".user-name").html(userName);
var myindex = 0;//全局变量
拼接这样写data-index="'+ myindex+'"
每调用一次函数myindex++;
最原始的,在外面套循环,然后把index穿进去,然后再循环里更改index的值