Ruby-on-rails 带有内联样式的 Rails link_to
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9768518/
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
Rails link_to with inline styling
提问by Marco
I must change a link_to tag color without using a CSS class, how can I do? I've tried something such as
我必须在不使用 CSS 类的情况下更改 link_to 标签颜色,我该怎么办?我尝试过诸如
<%= link_to item.description, {}, {:style=>'color:#FFFFFF;', :class => "css_class"} %>
but it doesn't works on ruby 1.9.2 and rails 3.1
但它不适用于 ruby 1.9.2 和 rails 3.1
回答by ksol
How about
怎么样
<%= link_to item.description, nil, {:style=>'color:#FFFFFF;', :class => "css_class"} %>
...or...
...或者...
<%= link_to item.description, '#', {:style=>'color:#FFFFFF;', :class => "css_class"} %>
回答by Onur Kucukkece
This should work with Rails 3
这应该适用于 Rails 3
link_to item.description, :style=> 'color:#FFFFFF;', :class => 'css_class'
With the new syntax in rails 4, it becomes
使用 rails 4 中的新语法,它变成
link_to item.description, style: 'color:#FFFFFF;', class: 'css_class'
回答by Diego Somar
I want to update this topic, because in this time, the syntax is different. In rails 4+, the correct syntax is:
我想更新这个话题,因为这次,语法不同。在 rails 4+ 中,正确的语法是:
<%= link_to TEXT, URL, class: 'css_class', style: 'color:#FFFFFF' %>
回答by tonymarschall
You can try link_to item.description, {}, {:style => 'color: #FFFFFF'}is ok.
你可以试试没问题link_to item.description, {}, {:style => 'color: #FFFFFF'}。
To color your links you have to set more then color:
要为链接着色,您必须设置更多color:
a:link {
color: #333333;
}
a:visited {
color: #FFFFFF;
}
a:hover {
color: #CCCCCC;
}
a:active {
color: #333333;
}
I recommend to use a css class for this.
我建议为此使用 css 类。
回答by Sherlock
try this:
尝试这个:
= link_to name, url, style: 'color:#FFFFFF;'
回答by Sobin Sunny
I am pretty sure this code will work.
我很确定这段代码会起作用。
<%= link_to "button_name",{:controller => 'controller_name', :action => 'action_name'},{:style=>"color:#fff;"}%>
<%= link_to "button_name",{:controller => 'controller_name', :action => 'action_name'},{:style=>"color:#fff;"}%>
回答by atw
If you have a class called test-color, you can assign the :hoverselector to that class by joining the class name and the :hover selector together.
如果您有一个名为 的类test-color,您可以:hover通过将类名和 :hover 选择器连接在一起来将选择器分配给该类。
Class hooks begin with a dot(.), IDs begin with a hash(#)
类钩子以点(.)开头,ID以哈希(#)开头
.test-color:link {
color: #333333;
}
.test-color:visited {
color: #FFFFFF;
}
.test-color:hover {
color: #CCCCCC;
}
.test-color:active {
color: #333333;
}
回答by Cody Elhard
link_to can be written as
link_to 可以写成
<%= link_to text, path, class: "" %>
Or
或者
<%= path, class: "" do %>
<div>
<!-- Insert HTML here -->
</div>
<% end %>

