我正在尝试接收消息,当我这样做时,除了html结束标记不返回(</div>
,</p>
等)之外,它大部分都有效。我使用jquery html()
,我使用text()
来验证结束标记是否不会返回。这是我的代码:
function formatMessages(data) {
var dataArray = data.split("\n");
var messages = "";
for (var i = 0; i < dataArray.length; i++) {
dataArray[i] = dataArray[i].split(":");
}
for (var i = 0; i < dataArray.length; i++) {
messages += '<div class="chatMessage"><h3 class="chatName">'+dataArray[i][0]+":</h3> ";
messages += '<h4 class="chatContent">'+dataArray[i][1]+'</h4>';
var timeDate = moment(dataArray[i][2]*1000).format("M/D/YY");
var timeMinute = moment(dataArray[i][2]*1000).format("m");
var timeHour = moment(dataArray[i][2]*1000).format("H");
if(timeHour>12) {
timeHour -=12;
var ampm = "pm"
}
else {
ampm = "am"
}
messages += '<h6 class="chatTime">'+ timeHour + ':' + timeMinute +'<span class="chatAmpm">' + ampm +'</span> (' + timeDate + ')</h6></div>';
}
return messages;
}
我用以下方式实现它:
$('#messages').html(formatMessages(data));
&#39;消息&#39;标记:
<div id="messages"></div>
数据示例:
Kyle Donoghue:g:1490379471
Kyle Donoghue:hell:1490379481
Kyle Donoghue:llklkl:1490380511
Kyle Donoghue:ljnlkn:1490380512
输出:
<div class="chatMessage"><h3 class="chatName">Kyle Donoghue: <h4 class="chatContent">g<h6 class="chatTime">11:17<span class="chatAmpm">am (3/24/17)<div class="chatMessage"><h3 class="chatName">Kyle Donoghue: <h4 class="chatContent">hell<h6 class="chatTime">11:18<span class="chatAmpm">am (3/24/17)<div class="chatMessage"><h3 class="chatName">Kyle Donoghue: <h4 class="chatContent">llklkl<h6 class="chatTime">11:35<span class="chatAmpm">am (3/24/17)<div class="chatMessage"><h3 class="chatName">Kyle Donoghue: <h4 class="chatContent">ljnlkn<h6 class="chatTime">11:35<span class="chatAmpm">am (3/24/17)