使用ajax显示所有消息通知

时间:2014-03-21 14:38:36

标签: php jquery html

我使用AJAX来显示消息通知,但问题是如果我同时向特定用户发送两条消息,他只会收到最新消息。 这是my_js.js文件:

function updatess() {
     $.getJSON("php/fetch.php", function(data) {
       $.each(data.result, function(){
        var name = this['sender_name'];
        var message = this['message'];

       $( '#messages' ).attr( 'id', 'new_msgs' );       
    $('div.from_name').html( name );
    $('div.message').html( message );
   });
 });
}

这是我的索引页面:

<div id="msg_holder" style=" overflow-x:scroll ;overflow-y: hidden; position:fixed;height:45px;width:865px;background-color:yellow;bottom:0px; left:144px; ">
<div class="mess_test" style="width:165px; height:35px; background:blue; bottom:0px; left:144px; margin-right: 16px">
<div class="from_name" style="float:left; "></div>
<div class="message" style="float:right;"></div>
</div>

1 个答案:

答案 0 :(得分:1)

.html()会重置元素的HTML,您可能希望改为使用 .append()

  

将参数指定的内容插入每个元素的末尾   在匹配元素集合中。

所以你可以改变:

$('div.from_name').html( name );
$('div.message').html( message );

为:

$('div.from_name').append( name );
$('div.message').append( message );