Html 什么字符代表文本区域中的新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14217101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What character represents a new line in a text area
提问by Ninjanoel
Just quick one, but want to make sure I'm catching cross platform variations.
只是一个快速的,但想确保我能捕捉跨平台的变化。
I like to convert new lines entered into a text area into a [comma], so that the output can be represented on a single line, my question...
我喜欢将输入文本区域的新行转换为 [逗号],以便输出可以在一行中表示,我的问题...
Currently, sending from google chrome, when I view the value, I find it uses \r\n
for new lines. If I replace \r\n
I know it will work for chrome on windows 7, but what about other platforms, are there variations on what other browsers will insert as a new line inside a text area?
目前,从谷歌浏览器发送,当我查看值时,我发现它\r\n
用于新行。如果我替换,\r\n
我知道它适用于 Windows 7 上的 chrome,但是对于其他平台,其他浏览器将作为新行插入文本区域内的内容是否有变化?
回答by Jukka K. Korpela
By HTML specifications, browsers are required to canonicalize line breaks in user input to CR LF (\r\n
), and I don't think any browser gets this wrong. Reference: clause 17.13.4 Form content typesin the HTML 4.01 spec.
根据 HTML 规范,浏览器需要将用户输入中的换行符规范化为 CR LF ( \r\n
),我认为任何浏览器都不会出错。参考:HTML 4.01 规范中的第 17.13.4 条表单内容类型。
In HTML5 drafts, the situation is more complicated, since they also deal with the processes inside a browser, not just the data that gets sent to a server-side form handler when the form is submitted. According to them (and browser practice), the textarea
element value exists in three variants:
在 HTML5 草稿中,情况更为复杂,因为它们还处理浏览器内部的流程,而不仅仅是提交表单时发送到服务器端表单处理程序的数据。根据他们(和浏览器的实践),textarea
元素值存在于三种变体中:
- the raw value as entered by the user, unnormalized; it may contain CR, LF, or CR LF pair;
- the internal value, called “API value”, where line breaks are normalized to LF (only);
- the submission value, where line breaks are normalized to CR LF pairs, as per Internet conventions.
- 用户输入的原始值,未标准化;它可能包含 CR、LF 或 CR LF 对;
- 内部值,称为“API 值”,其中换行符标准化为 LF(仅);
- 提交值,其中换行符标准化为 CR LF 对,根据 Internet 约定。
回答by Ben
Talking specifically about textareas in web forms, for all textareas, on all platforms, \r\n
will work.
专门讨论 Web 表单中的 textareas,对于所有 textareas,在所有平台上,\r\n
都将起作用。
If you use anything else you will cause issues with cut and paste on Windows platforms.
如果您使用其他任何东西,您将在 Windows 平台上导致剪切和粘贴问题。
The line breaks will be canonicalised by windows browsers when the form is submitted, but if you send the form down to the browser with \n
linebreaks, you will find that the text will not copy and paste correctly between for example notepad and the textarea.
提交表单时,Windows 浏览器将对换行符进行规范化,但是如果您将带有\n
换行符的表单发送到浏览器,您会发现文本将无法在例如记事本和文本区域之间正确复制和粘贴。
Interestingly, in spite of the Unix line end convention being \n
, the standard in most text-based network protocols including HTTP, SMTP, POP3, IMAP, and so on is still \r\n
. Yes, it may not make a lot of sense, but that's history and evolving standards for you!
有趣的是,尽管 Unix 行结束约定是\n
,但大多数基于文本的网络协议(包括 HTTP、SMTP、POP3、IMAP 等)的标准仍然是\r\n
。是的,这可能没有多大意义,但这是历史和不断发展的标准!
回答by barncat
It seems that, according to the HTML5 spec, the value property of the textarea element should return '\r\n' for a newline:
似乎,根据HTML5 规范, textarea 元素的 value 属性应该为换行符返回 '\r\n' :
The element's valueis defined to be the element's raw value with the following transformation applied:
Replace every occurrence of a "CR" (U+000D) character not followed by a "LF" (U+000A) character, and every occurrence of a "LF" (U+000A) character not preceded by a "CR" (U+000D) character, by a two-character string consisting of a U+000D CARRIAGE RETURN "CRLF" (U+000A) character pair.
元素的值被定义为应用了以下转换的元素的原始值:
替换每次出现的“CR”(U+000D)字符后不跟“LF”(U+000A)字符,以及每次出现的“LF”(U+000A)字符前不带“CR”( U+000D) 字符,由一个由 U+000D CARRIAGE RETURN "CRLF" (U+000A) 字符对组成的两字符字符串组成。
Following the link to 'value' makes it clear that it refers to the value property accessed in javascript:
按照“value”的链接,它清楚地表明它指的是在 javascript 中访问的 value 属性:
Form controls have a value and a checkedness. (The latter is only used by input elements.) These are used to describe how the user interacts with the control.
表单控件具有值和检查性。(后者仅由输入元素使用。)它们用于描述用户如何与控件交互。
However, in all five major browsers (using Windows, 11/27/2015), if '\r\n' is written to a textarea, the '\r' is stripped. (To test: var e=document.createElement('textarea'); e.value='\r\n'; alert(e.value=='\n');) This is true of IE since v9. Before that, IE was returning '\r\n' and converting both '\r' and '\n' to '\r\n' (which is the HTML5 spec). So... I'm confused.
但是,在所有五个主要浏览器中(使用 Windows,2015 年 11 月 27 日),如果将 '\r\n' 写入文本区域,则删除 '\r'。(要测试: var e=document.createElement('textarea'); e.value='\r\n'; alert(e.value=='\n');) 从 v9 开始,IE 就是如此。在此之前,IE 正在返回 '\r\n' 并将 '\r' 和 '\n' 转换为 '\r\n'(这是 HTML5 规范)。所以……我很困惑。
To be safe, it's usually enough to use '\r?\n' in regular expressions instead of just '\n', but if the newline sequence must be known, a test like the above can be performed in the app.
为安全起见,通常在正则表达式中使用 '\r?\n' 而不是 '\n' 就足够了,但是如果必须知道换行符序列,则可以在应用程序中执行类似上述的测试。
回答by Damodar Das
- Line Feed and Carriage Return
These HTML entitieswill insert a new line or carriage return inside a text area.
这些HTML 实体将在文本区域内插入新行或回车。