【发布时间】:2016-01-01 08:29:07
【问题描述】:
我试图在 ajax 调用后滚动一个 div,但它不起作用。 Jquery .scrollTop() not working 和 jQuery scrollTop function not working after ajax call 中提到的解决方案也不起作用。
问题在于这个函数调用:
$("#admin .boxcontent #logadmin")
.scrollTop($("#admin .boxcontent #logadmin")[0].scrollHeight);
它不会滚动 div。控制台也没有错误。
HTML:
<div class="panel-body">
<div id="admin">
<div class="boxcontent" style="white-space: pre-wrap;">
<div id="logadmin" clear="all" style="white-space: pre-wrap;word-wrap: break-word;"></div>
</div>
</div>
</div>
Javascript:
function waitForMsg()
{
$.get("poll.php", function(data)
{
if(data!='')
{
document.getElementById('logadmin').innerHTML=data;
console.log("scroll height:"+$("#admin .boxcontent #logadmin")[0].scrollHeight);
// Works perfectly. Scroll height gets printed in console
$("#admin .boxcontent #logadmin").scrollTop($("#admin .boxcontent #logadmin")[0].scrollHeight); //Does not work!!! No error in console.
/*setTimeout(function() {
$("#admin .boxcontent #logadmin").scrollTop($("#admin .boxcontent #logadmin")[0].scrollHeight);
}, 10); }*/
}
setTimeout("waitForMsg()",1000);
}).fail(function() {
setTimeout("waitForMsg()",5000);
});
}
CSS:
.boxcontent {
font-family: arial,sans-serif;
font-size: 13px;
color: #333333;
height:95%;
width:95%;
overflow-y:auto;
overflow-x:auto;
padding:7px;
border-left:1px solid #cccccc;
border-right:1px solid #cccccc;
border-bottom:1px solid #eeeeee;
background-color: #ffffff;
line-height: 1.3em;}
.panel-body {
height: 80vh;
overflow-y: scroll;}
【问题讨论】:
-
额外的
, 10);}是什么? -
那部分有注释。它是这篇文章中提到的解决方案:stackoverflow.com/questions/17578901/…。但这不起作用。
-
只是一个提示,我注意到你的选择器效率很低。应该只是
$("#logadmin"),因为那是一个 ID 选择器。 -
@PatrickRoberts 我试过了,但没用。
-
@user221458 这不是解决方案,只是建议。否则我会发布答案。
标签: javascript jquery html ajax scrolltop