簡體   English   中英

Rails 5:在鏈接上添加額外的字符

[英]Rails 5: Adding extra characters on the links

我在 rails 中創建了一個滾動導航,需要訪問root url + '#about'和頁面的類似部分。

這是我的 html:

<%= link_to "Home", root_path, class: "nav-link", :"data-id" => "home" %>
<%= link_to "About", root_path, class: "nav-link", :"data-id" => "about" %>
<%= link_to "Testimonials", root_path, class: "nav-link", :"data-id" => "testimonials" %>

這是js代碼:

 // Scrolling Nav
  $(".nav-link").click(function(){
      var topData = $( '#' + $(this).attr("data-id")).offset();
      $('html, body').animate({
          scrollTop: topData.top
      }, 'slow');
     });

默認情況下,這將滾動到帶有 data-id 或 div 內容部分 id 的部分homeabouttestimonials

在 Rails 中有沒有辦法可以在我的鏈接上插入root_path + 特定文本(如#about ,因此如果用戶單擊About鏈接之類的鏈接,它將解析 url http://localhost:3000#about

我試過這個:

<%= link_to "About", root_path#{#about}, class: "nav-link", :"data-id" => "about" %>

但是沒有用。 有沒有辦法做到這一點? 請幫忙!

我不確定我是否理解正確,但您應該能夠像這樣錨定到所需的文本:

root_path(:anchor => 'about')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM