我要添加我的消息,它会自动添加到文本区域并向下滚动。我有自己的代码。但它似乎不起作用。我也试过这段代码,但很难操纵。 Here
编辑:
发送
<textarea name="msg" id="area" ></textarea><br/>
<a href="#" onclick="submitChat()"> Send </a>
<div id="chatlogs">
</div>
var onKeyDown = function(e) {
if (e.which == 13) {
e.preventDefault();
submitChat();
$("#area").val("");
}
};
$("#area").on("keydown", onKeyDown);
function submitChat(){
if( form1.msg.value ==''){ //tell the function to exit if one of the fields are blank
alert("All fiels are mandatory");
return;
}
form1.uname.readonly=true;
form1.uname.style.border='none';
$('#imageload').show();
var uname= form1.uname.value; //storing the value pf fields into js variable which will pass on insert.php myimage
var msg = form1.msg.value;
var myimage = form1.myimage.value;
var room = form1.room.value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status ==200)
{
document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;
$('#imageload').hide();
}
}
xmlhttp.open('GET','insert.php?uname='+uname+'&msg='+msg+'&myimage='+myimage+'&room='+room,true);
xmlhttp.send();
}
$(document).ready(function(e)
{
$.ajaxSetup({cache:false});
setInterval(function() {$('#chatlogs').load('logs.php');}, 2000);
});
最佳答案
你可以使用
document.getElementById('chatlogs').lastChild.scrollIntoView(false)
如果您在单独的元素中有消息(您应该这样做)。
关于javascript - 添加新消息时自动向下滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29585884/