简体   繁体   English

如何防止网页回滚到浏览器顶部?

[英]How to prevent web page from scrolling back to the top of the browser?

I have a web page listing of items such as books, and it's been rendered by JavaScript, making requests to restful services. 我有一个网页,其中列出了诸如书籍之类的项目,并且该页面已由JavaScript渲染,从而请求了宁静的服务。 However, if I scroll down to the bottom of the browser, and select to view an item, and then press the browser back button, the page doesn't scroll down to the bottom. 但是,如果我向下滚动到浏览器的底部,然后选择要查看的项目,然后按浏览器的“后退”按钮,则该页面不会向下滚动到底部。 Instead, it scrolls to the top of the browser. 而是滚动到浏览器顶部。

How can I make it so that the web page scrolls back to the previous location? 如何使网页滚动回到上一个位置?

It depends on how you are implementing the events. 这取决于您如何实现事件。

I suppose you attached the event to a link and set the href attribute to "#". 我想您将事件附加到链接,并将href属性设置为“#”。 This is why the scroll goes back to the top. 这就是滚动返回顶部的原因。

If you are using jQuery, you can use the preventDefault() method from the Event object and it should work. 如果使用的是jQuery,则可以使用Event对象中的preventDefault()方法,它应该可以工作。 Otherwise, if you return false in the event handler, the action should stop. 否则,如果在事件处理程序中返回false ,则该操作应停止。

Can you provide some code of how you're calling the resful service? 您能否提供一些有关如何调用Resful服务的代码?

You should make sure that your links use the anchor tag (#), even if the page doesnt reload. 即使页面没有重新加载,也应确保链接使用锚标记(#)。 This way when user hits back they will be redirected to the last # and therefore the last position on the page. 这样,当用户回击时,他们将被重定向到最后的#号,因此也将重定向到页面上的最后一个位置。

http://www.w3schools.com/tags/tag_a.asp http://www.w3schools.com/tags/tag_a.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM