需要在 JavaScript 中生成 text
类型的属性。
一些记录有用户插入的回车符,这些回车符需要以 \n
形式传递给 JavaScript。然而当我放置
<%= uc.main_text %>
在 javascript 中,它返回回车符
Salties at bottom...
what happens here I am not certain
当需要\n
时。
Salties at bottom...\n\nwhat happens here I am not certain
从控制台调用属性返回正确的字符串
uc.main_text
[...] \r\n\r\n [...]
如何转置它以供 JavaScript 使用?
最佳答案
您需要#escape_javascript
:
string = "Salties at bottom...
what \"happens\" here I 'am' not certain"
puts %{var string = "#{escape_javascript(string)}"}
这将打印:
var string = "Salties at bottom...\n\nwhat \"happens\" here I \'am\' not certain"
关于javascript - JavaScript 的 Rails text_area 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56575752/