Javascript document.writeln 不写入新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5795843/
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
document.writeln doesn't write to a new line
提问by Mark Sandman
I was just writing some simple code and I noticed that using document.writeln doesn't write to a new line, permit me to demonstrate...
我只是在写一些简单的代码,我注意到使用 document.writeln 不会写入新行,请允许我演示......
// this is my JSON object
var myObject = {
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": [{
"Address1": "11 My Street",
"Address2" : "Nice Area",
"Town" : "Nice Town",
"PCode" : "P05T 0DE"
}]
}
document.writeln(myObject.firstName);
document.writeln(myObject.address[0].Address1);
now this outputs the following....
现在这输出以下....
John 11 My Street
约翰 11 号我的街
It's all on one line? If I used document.write() I'd expect this? It's happening in both IE & Firefox. Obviously I could add + "<br/>"
or + "\n"
but I shouldn't need to do that?
都在一条线上?如果我使用 document.write() 我会期待这个吗?它发生在 IE 和 Firefox 中。显然我可以添加+ "<br/>"
或+ "\n"
但我不需要这样做?
Am I being stupid?
我傻吗?
回答by Ignacio Vazquez-Abrams
HTML layout engines fold all whitespace to a single space. Of courseyou need <br />
or some other mechanism that HTML uses for putting things on separate lines.
HTML 布局引擎将所有空格折叠为一个空格。的当然你需要<br />
或其他一些机制,HTML用途把东西放在单独的行。
回答by mu is too short
document.writeln
does this:
document.writeln
做这个:
Writes a string of text followed by a newline character to a document.
将后跟换行符的文本字符串写入文档。
But whitespace is collapsed and converted to a single space when rendering HTML so your newline does nothing inside the HTML.
但是在渲染 HTML 时,空格会折叠并转换为单个空格,因此您的换行符在 HTML 中什么也不做。
回答by stealthyninja
It just appears to be all on one line. Do this --
它似乎都在一条线上。做这个 -
document.write('<pre>');
document.writeln(myObject.firstName);
document.writeln(myObject.address[0].Address1);
and you'll see.
你会看到的。
回答by kennebec
view the source of the page to see how the browser puts each line on a line-
but if you want to see a newline in the html, yes, put a '<br>'
between them.
查看页面的源代码以查看浏览器如何将每一行放在一行上——但如果你想在 html 中看到一个换行符,是的,'<br>'
在它们之间放一个。
回答by Vincent Ramdhanie
What you are outputting is being interpreted as HTML and you have to output the formatting tags to get it to look the way you want.
您输出的内容被解释为 HTML,您必须输出格式标签才能使其看起来像您想要的那样。
回答by ThatMatthew
I would expect that the source code of the page would have it on two lines, but the browser will condense it to one line since it condenses all white space to a single space. So in the browser, it displays it on one line.
我希望页面的源代码将它分为两行,但浏览器会将其压缩为一行,因为它将所有空白压缩为一个空格。所以在浏览器中,它显示在一行上。
As you said, you need to add a BR element to get it displayed on two lines.
正如您所说,您需要添加一个 BR 元素才能将其显示在两行上。
回答by Wooble
document.writeln()
doesappend a \n
to each line. However, in HTML whitespace is all displayed as a single space; you'll need to add <br>
or <p>
tags yourself.
document.writeln()
确实将 a 附加\n
到每一行。但是,在 HTML 中,空格全部显示为单个空格;您需要自己添加<br>
或<p>
标记。
回答by user1649798
To use document.write() or document.writeln() to get some type of a list or new text lines in an HTML document:
要使用 document.write() 或 document.writeln() 在 HTML 文档中获取某种类型的列表或新文本行:
var somevar = "foo";
for(i=1;i<4;i++){
document.write(i+". "+somevar+"<br>");
}
This will output:
这将输出:
- foo
- foo
- foo
- 富
- 富
- 富
回答by ashutosh benni
You just need to put JavaScript code in <pre>
tag.
您只需要将 JavaScript 代码放入<pre>
标签中。
<body>
<pre>
<script src="your_javascript.js"></script>
</pre>
</body>