开发者

Making a Web Chat with jQuery

开发者 https://www.devze.com 2022-12-11 22:04 出处:网络
I am trying to implement a browser based chat system with jQuery. I want to poll the server for new messages and append them to the bottom of a div. I have two problems.

I am trying to implement a browser based chat system with jQuery. I want to poll the server for new messages and append them to the bottom of a div. I have two problems.

  • I can't get it to append text to the div
  • 开发者_运维百科
  • I don't know how to keep the div scrolled to the bottom as the text gets appended

Here's the relevant clip of my HTML:

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


I'm not sure what you're missing here.

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

And how to scroll to the bottom of a div


Use below code to scrollto automatically:

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');
}
0

精彩评论

暂无评论...
验证码 换一张
取 消