如何在HTML中显示任意文本?

时间:2011-01-15 06:14:33

标签: javascript html ruby-on-rails

在我的Rails 3应用程序中,我有一个表单,要求用户写一个注释。例如,用户输入的文本可能是

@note = "This is a note\n\nwith whitespace."

如何将其实际呈现为

This is a note

with whitespace.

当我在视图中显示注释时? (我正在寻找一个通用的解决方案,而不是简单地用\n取代<br />等等。)

2 个答案:

答案 0 :(得分:2)

使用&lt; pre&gt;标记为@Moak建议

使用CSS进行换行:

pre {
  white-space: -moz-pre-wrap; /* Mozilla, supported since 1999 */
  white-space: -pre-wrap; /* Opera 4 - 6 */
  white-space: -o-pre-wrap; /* Opera 7 */
  white-space: pre-wrap; /* CSS3 
  word-wrap: break-word; /* IE 5.5+ */
}

答案 1 :(得分:0)

在您看来,要么:

<pre><%= @note %></pre>

或者:

<%= @note.gsub(/\n/, '<br />') %>