无法读取未定义的属性'scrollHeight'

时间:2016-05-08 17:28:48

标签: javascript php jquery html css

我开发了一个使用javascript和php的聊天系统,但问题是,当收到新邮件或邮件越来越多,并且当收到新邮件时它会滚动,用户必须滚动自定义才能转到底部所以我试图添加javascript以使滚动位于底部,但问题是它不工作,但也得到一个javascript错误

这是我得到的错误

未捕获的TypeError:无法读取未定义的属性'scrollHeight'

我也放置了一个ajax cache false来每2秒刷新一次聊天记录,以便通过jquery直接刷新来刷新页面不会看到新消息会影响这个吗?

这是我的代码

<div class="sidebar-content">
<?php 
    $get_query = mysqli_query($connection, "SELECT * FROM users WHERE uid != '".$_SESSION['uid']."'");
    while($data_user = mysqli_fetch_assoc($get_query)) {
?>
<div class="contact" onclick="get(<?php echo $data_user["uid"]; ?>);">
    <?php if(!empty($data_user["profile_picture"])) { ?>
    <img src="<?php echo $data_user["profile_picture"]; ?>" class="contact__photo" />
    <?php } else { ?>
    <img src="uploads/profile_img.jpg" class="contact__photo" />
    <?php } ?>
    <span class="contact__name"><?php echo $data_user["fullname"]; ?></span>
     <span class="chat__id"><?php echo $data_user["uid"]; ?></span>
    <span class="contact__status <?php if($data_user["online_status"] == "Online") { echo "online";} ?>"></span>
</div>
<?php } ?>
<div class="search">
    <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/142996/elastic-srch.png" alt="" class="search__img" />
    <input type="text" class="search__input" placeholder="Search" />
</div>
</div>
<div class="chat">
   <span class="<?php if($status == "Online") { echo 'online_stat';} else {echo "ofline_stat";} ?>"></span>
<span class="chat__back" onclick="reload();"></span>
<span class="chat__status">status</span>
<div class="chat__person">
  <span class="chat__online active"></span>
  <span class="chat__name" id="chat_name">Huehue Huehue</span>
   <span class="contact__id" id="contact__id">Huehue Huehue</span>
</div>
<div id="chat_messages">
</div>
<script>

    $.ajaxSetup({cache:false});
    var timeout = setInterval(reloadChat, 2000);    
    function reloadChat () {
        var rid = document.getElementById("contact__id").innerHTML;
         $('#msgs').load('includes/messages.php?receipt_id='+rid);
    }
    $('#chat-scroll').animate({
    scrollTop: $('#msgs').get(0).scrollHeight}, 2000);   
</script>

 </div>

1 个答案:

答案 0 :(得分:1)

更改此行

scrollTop: $('#msgs').get(0).scrollHeight}, 2000);

scrollTop: $('#msgs').eq(0).scrollHeight}, 2000);

scrollTop: $('#msgs')[0].scrollHeight}, 2000);