gpt4 book ai didi

javascript - JavaScript 的 Rails text_area 格式

转载 作者:行者123 更新时间:2023-12-02 23:29:36 25 4
gpt4 key购买 nike

需要在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com