简体   繁体   中英

Rails: Anchor Tag to jump to location on same page without sending a new request and having to re-render the page

This is how I thought you do this. For the place you want to jump to, you simply specify an id:

<h1 id="top_of_blogs_list">TOP OF WEBPAGE</h1>

And then if you want to jump there from somewhere on a webpage via a link, you simply specify an anchor tag for that link:

  <%= link_to "Back to Top", blogs_path(anchor: "top_of_blogs_list")%>

The issue is that this is sending a brand new request to the server and re-rendering the entire page. I do not want to send a new request to the server. I want to keep the current page as it is currently rendered, and simply jump up to where that id of "top_of_blogs_list" is specified.

Perhaps this must be done using javascript?

You don't need the blogs_path. This should suffice:

<%= link_to "Back to Top", '#top_of_blogs_list' %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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