文本区域中正确的换行符是什么?
我正在使用 javascript 将行添加到 textarea
。分隔行的正确换行符是什么?
我正在 Chrome(在 Windows 上)中进行测试,它们全部("\r"
、"\n"
、"\r\n"
)似乎对我有用。我猜它不会是 "\r"
,因为如果我是正确的,只有旧 Mac 会使用它。
那么这有一个标准吗(因为我不知道客户端是否使用*nix / Windows)?
I'm adding lines to a textarea
using javascript. What is the correct linebreak to separate the lines?
I'm testing in Chrome (on Windows) and they all ("\r"
, "\n"
, "\r\n"
) seem to work for me. I'm guessing it won't be "\r"
, because if I'm correct only old Macs use that.
So is there a standard for this (since I will not know whether the client uses *nix / Windows)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
大多数程序/浏览器都会为您纠正它。我见过它有任何区别的唯一地方是在电子邮件标头(其中
\r\n
是必需的)和控制台程序(其中\n
是换行符,< code>\r 是重写当前行)。为了“安全”,您应该始终使用
\r\n
,因为如果浏览器/程序仅使用其中之一,则另一个无论如何都是不可打印的字符。Most programs/browsers will correct it for you. The only place I've ever seen it make any difference is in email headers (where
\r\n
is required) and console programs (where\n
is newline and\r
is rewrite-current-line).To be "safe", you should always use
\r\n
, because if the browser/program only uses one of them, the other's a non-printable character anyway.我发现(Professional Javascript,Holzner,S.)行的结束方式取决于操作系统,对于 UNIX,这是
\n
,对于 MAC 和\r
>\r\n 对于 Windows。Kolink 是对的,为了安全起见,请使用:
\r\n
。I found (Professional Javascript, Holzner,S.) that the way line is ended depends on operating system, for UNIX this is
\n
,\r
for MAC and\r\n
for Windows.Kolink is right, to be safe use:
\r\n
.您稍后会在 html 环境中使用该值吗?如果是这样,您想存储它,因为它将以其最终状态使用。
Do you use the value at a later date in an html environment? If so you want to store it as it will be used in its final state.