javascript “document.write('hello world\n');”有什么区别 和“document.writeln('hello world');”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3786316/
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 is the difference between “document.write(‘hello world\n’);” and “document.writeln(‘hello world’);”?
提问by Jitendra Vyas
What is the difference between document.write(‘hello world\n');and document.writeln(‘hello world');?
document.write(‘hello world\n');和 和有什么不一样document.writeln(‘hello world');?
Edit
编辑
My question is what will be the difference of output.
我的问题是输出的差异是什么。
回答by DarkDust
Historically, writelnwas intended to handle different newline conventions, of with \nis only one.
从历史上看,writeln旨在处理不同的换行符约定, with\n只是其中之一。
There are different conventions for end-of-line. '\n'is the end-of-line marker on UNIX, '\r'on Mac (AFAIK not any more as it's now a UNIX) and '\r\n'is DOS/Windows. Using writelnshould automatically use the correct one on the desired platform in other languages, but I don't really know whether JavaScript's document.writelnautomatically uses the correct one automagically.
行尾有不同的约定。'\n'是 UNIX'\r'上的行尾标记,在 Mac 上(AFAIK 不再是因为它现在是 UNIX)并且'\r\n'是 DOS/Windows。Usingwriteln应该会在其他语言的所需平台上自动使用正确的,但我真的不知道 JavaScript 是否会document.writeln自动自动使用正确的。
回答by Andreas
The writeln() method is identical to the write() method, with the addition of writing a newline character after each statement.
writeln() 方法与 write() 方法相同,只是在每个语句后添加了一个换行符。
from w3schools.com
edit:for the sake of completeness :)
编辑:为了完整起见:)
回答by Spudley
In theory, writeln() appends a line feed to the end of your string.
理论上, writeln() 在字符串的末尾附加一个换行符。
In practice, since you're talking Javascript, there's little real difference if you're generating HTML, since HTML ignores extra white space anyway.
实际上,由于您在谈论 Javascript,因此生成 HTML 时几乎没有真正的区别,因为无论如何 HTML 都会忽略额外的空白。
回答by Katalonis
afaik there's no difference between them. You'll end up with a line, which has a "new-line-sign" at the end, so the next text youre gonna display will show up in the following line.
afaik 它们之间没有区别。您最终会得到一行,其末尾有一个“换行符”,因此您要显示的下一个文本将显示在下一行中。
if this is a tricky question sorry for my ignorance:)
如果这是一个棘手的问题,请原谅我的无知:)
回答by thomasmalt
document.write() writes to the document without appending a newline on the end. document.writeln() writes to the document appending a newline at the end.
document.write() 写入文档而不在末尾附加换行符。document.writeln() 写入文档并在末尾附加换行符。
回答by Anu
Lets take this is as an example:
让我们以这个为例:
Out put for write(): Hello World!Have a nice day!
为 write() 输出:Hello World!祝您有美好的一天!
Note that write() does NOT add a new line after each statement.
请注意, write() 不会在每个语句后添加新行。
Output for writeln(): Hello World! Have a nice day!
writeln() 的输出:Hello World!祝你今天过得愉快!
Note that writeln() add a new line after each statement.
请注意 writeln() 在每个语句后添加一个新行。
回答by Vinay Guru
document.writeyou will get the courser in the same line, but in document.writelnyou will get the courser in the next line.
document.write您将在同一行中获得 courser,但在document.writeln 中您将在下一行获得 courser。

