Javascript AJAX 请求后停止滚动到顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5449833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
stop scrolling to top after AJAX request
提问by Miroo
Possible Duplicate:
How do I stop a web page from scrolling to the top when a link is clicked that triggers javascript?
my browser scrolls to top after ajax request then i use
我的浏览器在 ajax 请求后滚动到顶部然后我使用
$('html, body').animate({ scrollTop: $('#loadMore').offset().top }, 2000);
to scroll back to my div. is there a way i stop it from scrolling to top from the beginning
滚动回我的 div。有没有办法阻止它从一开始滚动到顶部
回答by fl00r
you should add return false
to the end of your ajax call function. Or to the end of your inline call:
您应该添加return false
到 ajax 调用函数的末尾。或者到您的内联通话结束时:
<a href="#" onclick="someAjaxCall(); return false">Link</a>
回答by Andreas Wong
if you are calling the ajax via anchor click, try this:
如果您通过锚点单击调用 ajax,请尝试以下操作:
<a id="clicker" href="#">Click here to do ajax</a>
$('#clicker').click(function(e) { e.preventDefault(); $.get("/myurl/") })
回答by tilleryj
Attach the event handler with jQuery (not with the onclick attribute) and remove the href attribute altogether. The problem is that the browser is navigating when the link is clicked. I see this happen alot when onclick is used and there is an error in your event handler before preventing the default.
使用 jQuery(而不是使用 onclick 属性)附加事件处理程序并完全删除 href 属性。问题是当点击链接时浏览器正在导航。当使用 onclick 并且在阻止默认值之前事件处理程序中存在错误时,我看到这种情况经常发生。
Another way to fix it for sure is to not use an <a> but rather a <span>.
确定修复它的另一种方法是不使用 <a> 而是使用 <span>。
回答by mattsven
/* AJAX stuff here */
$('html, body').stop();