繁体   English   中英

使用 Rails 字符串插值在 HTML 中添加多个条件 CSS 类

[英]Add multiple conditional CSS classes in HTML with Rails string interpolation

我正在使用 Rails 并尝试根据查询字符串参数有条件地添加 div 高度。 v查询字符串参数有四个可能的值:1、2、3 或 4。当v=3v=4时,我想显示heading-row-2 class。 否则,我想显示heading-row class。

我正在寻找这两个方面的东西:

<%= content_tag :div, class: "row #{'heading-row-2' ? (params[:v] == 3 || 4) : 'heading-row'}"%>

<div class="row <%='heading-row-2' ? (params[:v] == 3 || 4) : 'heading-row'%> ">

请注意,我还需要:
- “行”标签。
-div 中添加更多 html。

您可以在任何情况下添加heading-row 如果参数v为 3 或 4,则将值-2插入 class 选项:

<%= content_tag :div, class: "row heading-row#{'-2' if params[:v].in?(['3', '4'])}" %>

<div class="row heading-row<%= '-2' if params[:v].in?(['3', '4']) %>">

第二种方式似乎更容易阅读。


注意

params[:v] == 3 || 4

可能不是您所期望的,因为它的意思是“如果 params[:v] 的值等于 3,则返回 true,否则返回 4”。

暂无
暂无

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

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