[英]How to add multiple CSS classes to HTML node in Closure template (soy)?
[英]Add multiple conditional CSS classes in HTML with Rails string interpolation
我正在使用 Rails 並嘗試根據查詢字符串參數有條件地添加 div 高度。 v
查詢字符串參數有四個可能的值:1、2、3 或 4。當v=3
或v=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.