Php 在ajax中使用set interval时滚动到顶部
我使用ajax获得了一些结果。现在我想滚动到顶部。现在滚动顶部不起作用,因为我使用了setInterval。当我滚动到顶部,然后自动向下滚动。如何修复它 index.phpPhp 在ajax中使用set interval时滚动到顶部,php,jquery,ajax,Php,Jquery,Ajax,我使用ajax获得了一些结果。现在我想滚动到顶部。现在滚动顶部不起作用,因为我使用了setInterval。当我滚动到顶部,然后自动向下滚动。如何修复它 index.php <!DOCTYPE html> <html> <head> <title>Title</title> </head> <body> <div style="width: 300px;height: 400px;margi
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<div style="width: 300px;height: 400px;margin: auto;border: 1px solid;overflow-y: scroll;" id="result"></div>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
function data()
{
$.ajax({
url: "data.php",
success: function(data)
{
$("#result").html(data);
$('#result').scrollTop($('#result')[0].scrollHeight);
}
})
}
setInterval(function(){
data();
},1000);
</script>
</body>
</html>
<?php
$i = 1;
$res = '';
while($i <= 50)
{
$res .= "<h2>Hello World !!</h2>";
$i++;
}
echo $res;
?>
标题
函数数据()
{
$.ajax({
url:“data.php”,
成功:功能(数据)
{
$(“#结果”).html(数据);
$(“#结果”).scrollTop($(“#结果”)[0]。scrollHeight);
}
})
}
setInterval(函数(){
数据();
},1000);
Data.php
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<div style="width: 300px;height: 400px;margin: auto;border: 1px solid;overflow-y: scroll;" id="result"></div>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
function data()
{
$.ajax({
url: "data.php",
success: function(data)
{
$("#result").html(data);
$('#result').scrollTop($('#result')[0].scrollHeight);
}
})
}
setInterval(function(){
data();
},1000);
</script>
</body>
</html>
<?php
$i = 1;
$res = '';
while($i <= 50)
{
$res .= "<h2>Hello World !!</h2>";
$i++;
}
echo $res;
?>
我认为$(“#result')[0]中没有任何数据。scrollHeight
。你能把一些像50这样的伪值放在一边,检查它不工作吗?只使用$(“#result”).scrollTop()
,它应该可以工作,因为scrollTop方法不接受任何参数。@RohitMittal它接受:@RohitMittal,我在我的评论中链接了什么:->。scrollTop(value)
?我认为中没有任何数据$(“#结果”)[0]。scrollHeight
。你能把一些像50这样的伪值放在一边,检查它是否工作吗?只使用$(“#结果”)。scrollTop()
,它应该工作得很好,因为scrollTop方法不接受任何参数。@RohiMittal它接受:@RohiMittal,我在我的评论中链接了什么:->。scrollTop(值)
?