如何根据条件更改字体颜色? [英] How to Change Font Color based on Conditional?

查看:76
本文介绍了如何根据条件更改字体颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在 _form

    <%= f.text_field :result_value %>
    <%= f.date_select :date_value %>
    <%= f.check_box :good %>

我们如何将结果索引 (:result_value & :date_value) 中的字体颜色渲染为绿色?

how can we render the font color in the index of that result (:result_value & :date_value) to green?

result.good 下面在索引中拉出 true 或 false.一旦我们用颜色表示真假,我就会把它去掉:真 = 绿色,假 = 红色(默认颜色).

result.good below pulls up true or false in the index. I'll take that out once we represent true or false with colors: true = green, false = red (default color).

        <% averaged.results.each do |result| %>
          <li>
            <%= result.result_value %>
            <%= result.date_value.strftime("%b %Y") %>
            <%= result.good %>
          </li>
        <% end %>

.

class Result < ActiveRecord::Base
belongs_to :user
  belongs_to :quantified
  has_many :comments, as: :commentable
  default_scope { order('date_value DESC') }
  scope :good, -> { where(good: true) }
  scope :good_count, -> { good.count }
end

非常感谢您的帮助.

推荐答案

您可以根据输出向 LI 添加一个类吗?

You could add a class to the LI based on the output?

<% if result.good == true %>
    <li class="green">
<% else %>
    <li>
<% end %>

然后在你的 css 中为那个类设置一个颜色?

Then in your css just set a colour for that class?

.green {
    color: green;
}

这篇关于如何根据条件更改字体颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆