for (var key in data) {
$(".chat").html('<li class="\ left clearfix message \"></li>');
$(".message").html(data[key].message);
}
响应数据
[{"id":1,"message":"message 3","taker_id":"124","giver_id":"102","status":"0","stamp":"2016-08-24"},
{“id”:5,“message”:“message 2”,“taker_id”:“124”,“giver_id”:“102”,“status”:“0”,“stamp”:“2016- 08-17 “},{” ID” :6,“消息”:“这是新的消息测试服务现在我们没事”,“taker_id”:“124”,“giver_id”:“102”,“状态” : “0”, “图章”: “2016年8月11日”}]
输出
<li class=" left clearfix message ">This is the new message test service now we are ok</li>
问题: html
请帮忙!
答案 0 :(得分:-1)
使用html(),您只需在每次迭代时替换内容。你需要追加
var resp = [{
"id": 1,
"message": "message 3",
"taker_id": "124",
"giver_id": "102",
"status": "0",
"stamp": "2016-08-24"
}, {
"id": 5,
"message": "message 2",
"taker_id": "124",
"giver_id": "102",
"status": "0",
"stamp": "2016-08-17"
}, {
"id": 6,
"message": "This is the new message test service now we are ok",
"taker_id": "124",
"giver_id": "102",
"status": "0",
"stamp": "2016-08-11"
}]
resp.forEach(function(obj) {
$(".chat").append('<li class="left clearfix message ">' + obj.message + '</li>');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="chat"></ul>
&#13;
如果您真的不想在字符串中包含文本,那么您需要执行以下操作:
resp.forEach(function(obj) {
var li = $('<li class="left clearfix message "></li>');
li.text(obj.message);
$(".chat").append(li);
});