【发布时间】:2020-02-29 07:16:29
【问题描述】:
'''
<html>
<head>
<title>Auto Refresh</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script>
window.onload=function () {
var objDiv = document.getElementById("load_chats");
objDiv.scrollTop = objDiv.scrollHeight;
};
</script>
<style>
#load_chats{
width: 95%;
margin: auto;
height: 400px;
overflow-y:auto;
border: 2px solid black;
background-image: url("../a/images/paper_pic.jpg");
}
#chat_window{
width: 95%;
margin: auto;
}
#right{
border-radius: 5px;
border: 2px solid darkslateblue;
width: 40%;
min-height: 20px;
padding: 10px;
margin: 5px;
float: right;
display: inline;
clear: both;
word-wrap: break-word;
background-color: white;
}
#left{
border-radius: 5px;
border: 2px solid darkslateblue;
width: 40%;
min-height: 20px;
padding: 10px;
margin: 5px;
clear: both;
word-wrap: break-word;
background-color: white;
}
#date{
text-align: right;
}
p{}
</style>
</head>
<body>
<div id="chat_window">
<div id="load_chats"></div>
</div>
<form name="add_chat" method="post">
<label><textarea name="chat_content" id="chat_content"></textarea></label>
<input type="button" name="chat_button" id="chat_button" value="Chat"/>
</form>
</body>
</html>
<script>
$(document).ready(function(){
$('#chat_button').click(function(){
var tweet_txt = $('#chat_content').val();
//trim() is used to remover spaces
if($.trim(tweet_txt) !== '')
{
$.ajax({
url:"insert.php",
method:"POST",
data:{chat_content:tweet_txt},
dataType:"text",
success:function(data)
{
$('#chat_content').val("");
}
});
}
});
setInterval(function(){//setInterval() clearInterval()
$('#load_chats').load("fetch.php").fadeIn("slow");
}, 1000);
});
</script>
'''
所以我使用了我的部分代码
'''
<script>
window.onload=function () {
var objDiv = document.getElementById("load_chats");
objDiv.scrollTop = objDiv.scrollHeight;
};
</script>
...
在另一个页面上,它工作得很好,但是当我把它移到这个页面上时它不起作用 任何帮助都是极好的。我正在进行一个从 sql 数据库更新的聊天,它只是希望它从文本的底部开始,就像你在手机上看到的那样,我一直在查找它,但没有任何工作让我回到 window.onload=function
【问题讨论】:
标签: javascript html autoscroll