代码之家  ›  专栏  ›  技术社区  ›  The.Anti.9

使用jquery进行网络聊天

  •  1
  • The.Anti.9  · 技术社区  · 15 年前

    我正在尝试用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>
    
    2 回复  |  直到 13 年前
        1
  •  3
  •   Ben Shelock    15 年前

    我不知道你在这里遗漏了什么。

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

    以及如何 scroll to the bottom of a div

        2
  •  0
  •   eeerahul Arrj    13 年前

    使用以下代码自动滚动到:

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