1

我正在尝试根据元素在列表中的索引来分配元素的 CSS。

在这里,我将 CSS(“q-light”或“q-dark”)存储在变量“styling1”中,效果很好。

    <% answers.each_with_index do |answer, i| %>
    <% styling1 = i % 2 == 0 ? 'q-light' : 'q-dark' %>

    <div class="<%= styling %>">

但是,当我尝试将索引存储在变量 'styling2' 中并尝试将其添加到另一个 CSS 类时,假设每个元素的 CSS 将是 'otherclass 0'、'otherclass 1' 等等,它给了我一个语法错误,说我有一个意外的'>'。

    <% answers.each_with_index do |answer, i| %>
    <% styling2 = i %>

    <%= f.radio_button :answer_id, answer.id%>
    <%= f.label :answer_id, answer.answer, :class => "<%= otherclass styling =>" %> 

我不太确定错误来自哪里。我知道 CSS 类不应该以数字开头,但我用 unicode 转义字符命名它们,所以在我的 css 文件中它不是 0,而是 \30,并且该样式在其他地方有效,所以我不认为它是数字. 难道是当我分配像“:class =>”这样的类时我不能调用变量吗?

4

1 回答 1

2

使用"#{otherclass styling}"而不是<%= otherclass styling %>.

于 2012-12-10T04:40:43.583 回答