1

我每 3 秒更新一次聊天框并溢出 div(chatbody) 。我想要 evry 循环这个函数滚动 div 底部?

我的代码

function chat()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("chatbody").innerHTML=xmlhttp.responseText;
I THINK CODE MUST BE HERE 
    }
  }
  xmlhttp.open("GET","func/chat.php?tnow=" + (new Date()).getTime(),true);
xmlhttp.send();
}
setInterval("chat()",3000);
4

1 回答 1

1
function chat()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("chatbody").innerHTML=xmlhttp.responseText;
     document.getElementById("chatbody").scrollTop = document.getElementById("chatbody").scrollHeight; //This scrolls the chatbox to the bottom/last
    }
  }
  xmlhttp.open("GET","func/chat.php?tnow=" + (new Date()).getTime(),true);
xmlhttp.send();
}
setInterval("chat()",3000);

希望这可以帮助!

于 2012-05-17T07:42:50.397 回答