【问题标题】:How to write conditional class in html? [duplicate]如何在html中编写条件类? [复制]
【发布时间】:2017-05-24 02:46:50
【问题描述】:

comment 为 nil 时,我希望没有 id。 这是实际代码(不起作用)。问题是即使comment 不为零,它也会进入第二个条件并向我发送空 ID

<div id="<% comment ? 'id1-<%= comment.id %>-container' : '' " class="class2">

【问题讨论】:

  • 在 Stack Overflow 上提问并不能代替学习编程语言的基础知识(在本例中是 ERB),甚至不能使用搜索引擎。请查找有关如何使用 ERB 的教程并掌握基础知识。

标签: html ruby-on-rails ruby class


【解决方案1】:
<% if comment.nil? %> 
  <div class="class2" />
<% else %>
  <div id="id1-#{comment.id}-container" class="class2" />
<% end %>

【讨论】:

  • 我想在一行中完成这就是为什么我找不到解决方案。但我想我会接受你的解决方案并经历其他事情。感谢您的快速回答!
  • 单行很容易,看一下重复的问题。我只是发现在erb 中使用三元运算符绝对是可怕的,但这只是我的看法。
猜你喜欢
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
  • 2016-08-19
  • 1970-01-01
  • 2021-05-22
  • 2014-04-02
  • 2012-02-27
  • 1970-01-01
相关资源
最近更新 更多