简体   繁体   English

具有<a>标签</a>的link_to标记内的图标

[英]Icon within a link_to tag with <a> tags

I have a navbar button: 我有一个navbar按钮:

<%= link_to new_post_path do %>
  <a class="btn-navbar"><i class="fa fa-plus" aria-hidden="true"></i></a>
<% end %>

It does not allow me to click it and navigate to the correct page. 它不允许我单击它并导航到正确的页面。

If I remove the <a> tags, it works but I need the <a> because its have the correct class associated with it to style the button. 如果删除<a>标记,它可以工作,但是我需要<a>因为它具有与之关联的正确类来为按钮设置样式。

The above code is styled correctly but when trying to click it it does not link to the right page. 上面的代码样式正确,但是在尝试单击它时,未链接到正确的页面。

Add the class to the link to helper to have it applied to the a tag. 将类添加到帮助程序的链接,以将其应用于a标签。

<%= link_to new_post_path, class: 'btn-navbar' do %>
  <i class="fa fa-plus" aria-hidden="true"></i>
<% end %>

If you're using the official font-awesome rails gem: 如果您使用的是官方字体超赞的Rails gem:

https://github.com/FortAwesome/font-awesome-sass https://github.com/FortAwesome/font-awesome-sass

<%= link_to icon('plus'), new_post_path, class: 'btn-navbar' %>

用span替换标签,然后为span赋予与给tag相同的类,这应该给您相同的结果

<span class="btn-navbar">your code goes here</span>

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

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