div 加载时自动滚动到页面底部

div auto scrolls on load to the bottom of the page

'''
<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 数据库更新,它只是希望它从文本底部开始,就像您在 phone 上看到的那样,我一直在查找它,但没有任何效果让我返回 window.onload=函数

当页面刚刚加载时 div 的高度是 0,因为它是空的。所以你将它滚动到 0。在 将 fetch.php 加载到其中后,您必须将 div 滚动到底部

 $('#load_chats').load("fetch.php", {}, function(data){
        var objDiv = $("#load_chats")[0];
        objDiv.scrollTop = objDiv.scrollHeight;
 }).fadeIn("slow");