gpt4 book ai didi

javascript - ajax调用后滚动到某个元素

转载 作者:行者123 更新时间:2023-11-30 08:33:30 25 4
gpt4 key购买 nike

我试图在 ajax 调用后滚动到页面上的某个元素,但由于某种原因它不起作用。我究竟做错了什么?

测试.php

<style>
#divOne {
border: 1px solid red;
height: 100%;
width: 100%;
}
#divTwo {
border: 1px solid blue;
height: 100%;
width: 100%;
}
</style>

<input id = 'click' type = 'submit' value = 'Click' onclick = "ajaxCall('testx.php')">
<div id = 'divOne'></div>
<div id = 'divTwo'></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">

function ajaxCall(action) {

$.ajax({
type: "POST",
url: action,
error: function(xhr,status,error){alert(error);},
success:function(data) {
document.getElementById('divTwo').innerHTML = data;
}, //end of success:function(data)
complete:function(data) {
$("#click").click(function (){
$('html, body').animate({
scrollTop: $("#divTwo").offset().top
}, 2000);
} //end of complete:function(data)
}); //end of $.ajax({

} //end of function ajaxCall()

</script>

testx.php

<?php

echo "Hello World!";

?>

预期结果:

Hello World! 
(The page to scroll to #divTwo)

实际结果:

Hello World! 
(The page DID NOT scroll to #divTwo)

最佳答案

您的完整功能只是定义一个点击处理程序,而不是实际执行滚动。只需将执行滚动的代码放在 .click() 中即可。

    complete:function(data) {
$('html, body').animate({
scrollTop: $("#divTwo").offset().top
}, 2000);
}

关于javascript - ajax调用后滚动到某个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34503615/

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