2009-11-21 86 views
1

我正试图用jQuery实现基于浏览器的聊天系统。我想轮询服务器的新消息,并将它们追加到div的底部。我有两个问题。与jQuery进行网上聊天

  • 我不能让它将文本追加到div
  • 我不知道如何保持滚动至底部的DIV作为文本将追加

这里的相关片段我的HTML:

<div id="main"> 
<form action='post.php' method='post'> 
    <div id='messages'>stuff</div><br /> 
    <input type='text' name='usertext' /> 
</form> 
</div> 

回答

3

我不确定你在这里错过什么。

$(selector).append('<div class="message">sometext</div>'); 

而如何scroll to the bottom of a div

+0

您提供的代码仅仅基于,Ben的例子将扩大到$( '格#主要的div#消息' ).append('

sometext
'); – eidylon 2009-11-21 06:02:27

0

下面的代码使用自动scrollto:

var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; 
//chatbox is the id of div 
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; 
if(newscrollHeight > oldscrollHeight) 
{ 
    $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); 
}