2009-11-21 53 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'); 
}