为什么 javascript 换行符在 html 中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5782409/
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
Why doesn't javascript newlines work inside html?
提问by user723636
So I have the following:
所以我有以下几点:
<html> <body> <script type="text/javascript"> document.write('Hello\nWorld') </script> </body> </html>
As you probably all know \n doesn't work and I have to use <br> instead. It won't work either if I link to an external js file. Here are my questions:
你们可能都知道 \n 不起作用,我必须使用 <br> 代替。如果我链接到外部 js 文件,它也不会工作。以下是我的问题:
1) Why doesn't \n
work?
2) Why does <br> even work? Shouldn't everything that's inside the script
tags be strictly javascript instead of a dirty mix between html and js?
3) Is it possible to make \n
work somehow?
4) I know \t
doesn't work either. Any other stuff that won't work inside
html files?
5) Unrelated question (I didn't want to open a new question just for this).
I installed node.js to be able to try out js scripts from inside vim but
when I run this script I get the error "document is not defined". Same thing happens when I try from the repl. Any ideas?
1)为什么不起作用\n
?
2) 为什么 <br> 甚至可以工作?脚本标签内的所有内容不应该是严格的 javascript 而不是 html 和 js 之间的肮脏混合吗?
3)是否有可能以\n
某种方式工作?
4)我知道\t
也行不通。任何其他在 html 文件中不起作用的东西?
5)不相关的问题(我不想为此打开一个新问题)。我安装了 node.js 以便能够从 vim 内部尝试 js 脚本,但是当我运行这个脚本时,我收到错误“文档未定义”。当我从 repl 尝试时,也会发生同样的事情。有任何想法吗?
Btw, I know these questions have probably been asked a million times but I honestly couldn't find good answers, all I got was that I should use <br>
instead of \n
. Thanks for any help.
顺便说一句,我知道这些问题可能已经被问了一百万次,但老实说我找不到好的答案,我得到的只是我应该使用<br>
而不是\n
. 谢谢你的帮助。
采纳答案by davin
\n
works, if you have a debugger of sorts (or similar developer tool) you can see the document source, and you will see that there is indeed a newline character. The problem is the way you are looking at the page - you're notreading it's source, you're reading it as an html document. Whitespace in html is compressed into a single space. So when you change the source, it does indeed change, although when interpreted as an html document, that change isn't shown.
\n
有效,如果您有各种调试器(或类似的开发人员工具),您可以看到文档源,并且您会看到确实有一个换行符。问题在于您查看页面的方式 - 您不是在阅读它的源代码,而是将其作为 html 文档阅读。html 中的空格被压缩成一个空格。因此,当您更改源时,它确实会发生变化,尽管当解释为 html 文档时,不会显示该更改。
Your node.js error is most probably caused by the fact that you're running browser scripts on the server. I.e. scripts that refer to the document
are intended to be run in a browser, where there is a DOM etc. Although a generic node process doesn't have such a global object because it isn't a browser. As such, when you try and run code that references a global object called document on the assumption that it exists just like in the browser, it will throw an error. document.write
doesn't exist, if you want to write to the screen, try console.log
or look into the other util functions.
您的 node.js 错误很可能是由于您在服务器上运行浏览器脚本造成的。即引用 的脚本document
旨在在浏览器中运行,其中有一个 DOM 等。虽然通用节点进程没有这样的全局对象,因为它不是浏览器。因此,当您尝试运行引用名为 document 的全局对象的代码时,假设它就像在浏览器中一样存在,它将抛出错误。document.write
不存在,如果您想写入屏幕,请尝试console.log
或查看其他 util 函数。
回答by Quentin
Why doesn't \n work?
为什么 \n 不起作用?
Because white space is just white space in HTML.
因为空白只是 HTML 中的空白。
Why does
<br>
even work?
为什么
<br>
甚至有效?
Because it is the HTML for a line break
因为它是换行符的 HTML
Shouldn't everything that's inside the script tags be strictly javascript instead of a dirty mix between html and js?
脚本标签内的所有内容不应该是严格的 javascript 而不是 html 和 js 之间的肮脏混合吗?
That's subjective. document.write
is considered dirty by many as well.
那是主观的。document.write
也被许多人认为是肮脏的。
You can always use createElementand createTextNode
您始终可以使用createElement和createTextNode
Is it possible to make \n work somehow?
是否有可能使 \n 以某种方式工作?
I know \t doesn't work either. Any other stuff that won't work inside html files?
我知道 \t 也不起作用。任何其他在 html 文件中不起作用的东西?
HTML is not plain text. Listing all the differences would be time consuming, and out of scope for StackOverflow. Try reading the specification.
HTML 不是纯文本。列出所有差异将非常耗时,并且超出 StackOverflow 的范围。尝试阅读规范。
Unrelated question (I didn't want to open a new question just for this).
不相关的问题(我不想为此打开一个新问题)。
It is completely unrelated. Open a new question.
这是完全无关的。打开一个新问题。
回答by Chris Kooken
When HTML renders, it ignores whitespace. Thus the only way is to use line breaks.
当 HTML 呈现时,它会忽略空格。因此,唯一的方法是使用换行符。
Use <br/>
instead of \n
and it will work fine
使用<br/>
而不是\n
它会正常工作
The document.write()
function writes HTML into the element.
该document.write()
函数将 HTML 写入元素。
回答by BraedenP
When you write to the page, you're not writing JavaScript; you're writing HTML. \n is a special "line feed" character that doesn't create a line break in the browser's rendering of the HTML. It WILL create a line break in the HTML file itself, but the browser doesn't take this into consideration when it renders out the markup.
当您写入页面时,您并不是在编写 JavaScript;您正在编写 HTML。\n 是一个特殊的“换行”字符,它不会在浏览器呈现的 HTML 中创建换行符。它会在 HTML 文件本身中创建一个换行符,但浏览器在呈现标记时不会考虑这一点。
Thus, the br tag is required.
因此,需要 br 标签。
回答by Spliffster
Use the html tag <br/>
to input line endings (your output is interpreted by an html parser), for example:
使用 html 标签<br/>
输入行结尾(您的输出由 html 解析器解释),例如:
Javascript:
Javascript:
document.write("Hello<br/>World");
回答by Jonathan Wood
Whitespace emitted by JavaScript works like any other whitespace in your HTML file. That seems the expected behavior to me.
JavaScript 发出的空格与 HTML 文件中的任何其他空格一样。这对我来说似乎是预期的行为。
回答by Adam Bergmark
1,3. "\n" does work. If you do a document.write inside the body tag you can check document.body.innerHTML and see that the line break is indeed there.
1,3。"\n" 确实有效。如果您在 body 标签内执行 document.write,您可以检查 document.body.innerHTML 并查看是否确实存在换行符。
4.Anything HTML specific will be rendered HTML specific, so you will have to escape <
and >
into <
and >
for instance.
4.Anything HTML具体将呈现HTML特殊,所以你将不得不逃避<
和>
成<
和>
的实例。
5.document
is an object available in browsers, it is not used in node since the DOM doesn't exist there. require("sys");
and use sys.print in nodejs.
5.document
是浏览器中可用的对象,它不在节点中使用,因为那里不存在 DOM。require("sys");
并在 nodejs 中使用 sys.print。
回答by Mark Cidade
The document
object represents an HTML document; any text written to the document will be processed by the browser's HTML renderer. In HTML, all adjacent whitespace, including line breaks (e.g., "\n"
), are collapsed into a single space when rendered. This is why you need <br />
, which is rendered as a line break. You can make \n
work by replacing it with <br />
or by writing into a <pre>
element:
该document
对象代表一个 HTML 文档;写入文档的任何文本都将由浏览器的 HTML 渲染器处理。在 HTML 中,所有相邻的空格,包括换行符(例如,"\n"
),在渲染时都会折叠成一个空格。这就是为什么你需要<br />
,它被呈现为换行符。您可以\n
通过替换<br />
或写入<pre>
元素来工作:
Hello World
回答by mattsven
1) \n
works in plain text files.
1)\n
在纯文本文件中工作。
2) <br />
works because you are doing HTML in string. Strings can hold characters without breaking the rest of the JS script.
2)<br />
有效,因为您在字符串中执行 HTML。字符串可以保存字符而不会破坏 JS 脚本的其余部分。
3) Not really. Perhaps when you are using <textarea>
s.
3) 不是真的。也许当您使用<textarea>
s 时。
4) Most other \*
's
4) 大多数其他\*
的
5) More info needed.
5)需要更多信息。
回答by NATHAN SILVA SANTOS
Use <pre></pre>
on the html and it will respect the text format from JS.
<pre></pre>
在 html 上使用,它将尊重来自 JS 的文本格式。