gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 16:19:28 26 4
gpt4 key购买 nike

我已经使用 javascript 和 php 开发了一个聊天系统,但问题是当收到新消息或消息越来越多并且当收到新消息时它会滚动,用户必须滚动自定义才能转到底部,所以我尝试添加 javascript 以使滚动条位于底部,但问题是它无法正常工作,而且还会出现 javascript 错误

这是我得到的错误

未捕获的类型错误:无法读取未定义的属性“scrollHeight”

我还设置了一个 ajax 缓存 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>

最佳答案

改变这一行

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

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

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

关于javascript - 无法读取未定义的属性 'scrollHeight',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37102733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com