Html 在 Rails link_to 中添加 span 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7563911/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:48:45  来源:igfitidea点击:

Adding span tag in Rails link_to

htmlruby-on-railsruby-on-rails-3link-to

提问by tvalent2

I've looked on SO about how to add a <span>tag but I didn't see an example that placed the <span>where I want using Rails 3 link_to:

我已经查看了关于如何添加<span>标签的SO,但我没有看到一个示例,该示例将<span>使用 Rails 3放置在我想要的位置link_to

<a href="#" class="button white"><span id="span">My span&nbsp;</span>My data</a>

I tried something like:

我试过类似的东西:

<%= link_to(content_tag{:span => "My span&nbsp;", :id => "span"} @user.profile.my_data, "#", {:class => "button white"}) %>

But that didn't work.

但这没有用。

回答by mu is too short

link_tocan take a blockso I think you're after something like this:

link_to可以占用一个块,所以我认为你在追求这样的事情:

<%= link_to '#', :class => 'button white' do %>
    <span id="span">My span&nbsp;</span><%= @user.profile.my_data %>
<% end %>

回答by Joe Susnick

A combination of the .html_safewith #{@user.profile.my_data}should work as well.

.html_safewith的组合也#{@user.profile.my_data}应该起作用。

<%= link_to "<span id='span'>My span&nbsp;</span>#{@user.profile.my_data}".html_safe, "#", class: 'button white' %>

You can also use a content_tagso it would look like:

您也可以使用 acontent_tag使其看起来像:

<%= link_to(content_tag(:span, "My span&nbsp;", id:"span")+"#{@user.profile.my_data}", "#", class: 'button white' %> 

They're basically identical but one might be easier on the eyes for you. Also, I'm pretty new to coding so if this is dead wrong for some crazy reason, please just comment and I'll change it. Thanks.

它们基本上相同,但对您来说可能更容易。另外,我对编码还很陌生,所以如果由于某些疯狂的原因这是完全错误的,请发表评论,我会更改它。谢谢。

回答by Arun Kumar Arjunan

link_to '#', :class => 'button white' do
  <span id="span">My span&nbsp;</span>My data
end

回答by mdev

In HAML :

在 HAML 中:

= link_to  new_post_mobile_path(topic.slug), class: 'add-new-place-btn' do
  %span{:class => "glyphicon glyphicon-plus", :style => "margin-right: 4px;"}
  New Place