admin管理员组文章数量:1303335
I'm trying to scroll to a certain element on the page after an ajax call, but it's not working for some reason. What am I doing wrong?
test.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=".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)
plete:function(data) {
$("#click").click(function (){
$('html, body').animate({
scrollTop: $("#divTwo").offset().top
}, 2000);
} //end of plete:function(data)
}); //end of $.ajax({
} //end of function ajaxCall()
</script>
testx.php
<?php
echo "Hello World!";
?>
Expected Result:
Hello World!
(The page to scroll to #divTwo)
Actual Result:
Hello World!
(The page DID NOT scroll to #divTwo)
I'm trying to scroll to a certain element on the page after an ajax call, but it's not working for some reason. What am I doing wrong?
test.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./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)
plete:function(data) {
$("#click").click(function (){
$('html, body').animate({
scrollTop: $("#divTwo").offset().top
}, 2000);
} //end of plete:function(data)
}); //end of $.ajax({
} //end of function ajaxCall()
</script>
testx.php
<?php
echo "Hello World!";
?>
Expected Result:
Hello World!
(The page to scroll to #divTwo)
Actual Result:
Hello World!
(The page DID NOT scroll to #divTwo)
Share
Improve this question
edited Dec 29, 2015 at 2:58
Barmar
783k56 gold badges546 silver badges660 bronze badges
asked Dec 29, 2015 at 2:33
jessicajessica
1,6872 gold badges20 silver badges37 bronze badges
15
- @barmar I need to execute this AFTER an ajax call. – jessica Commented Dec 29, 2015 at 2:39
- Why do you think the code in that question won't work after an AJAX call? – Barmar Commented Dec 29, 2015 at 2:41
- @barmar Ok. I will re-ask this question, after I try to use the code in the question, and it doesn't work again. – jessica Commented Dec 29, 2015 at 2:43
- Just give your elements some actual height. Instead of height:100% use height:400px for example. – Larry Lane Commented Dec 29, 2015 at 2:45
- @jessica Don't reask. If it doesn't work, update the question and request that it be reopened. – Barmar Commented Dec 29, 2015 at 2:48
1 Answer
Reset to default 10Your plete function is just defining a click handler, not actually performing the scroll. Just put the code that does the scroll, without putting it inside .click()
.
plete:function(data) {
$('html, body').animate({
scrollTop: $("#divTwo").offset().top
}, 2000);
}
本文标签: javascriptScrolling to a certain element after ajax callStack Overflow
版权声明:本文标题:javascript - Scrolling to a certain element after ajax call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741749615a2395746.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论