使用jQuery进行Web聊天

时间:2009-11-21 05:43:31

标签: jquery html chat

我正在尝试使用jQuery实现基于浏览器的聊天系统。我想在服务器上轮询新消息并将它们附加到div的底部。我有两个问题。

  • 我无法将其附加到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>

2 个答案:

答案 0 :(得分:3)

我不确定你在这里缺少什么。

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

以及如何scroll to the bottom of a div

答案 1 :(得分:0)

使用以下代码自动滚动:

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');
}
相关问题