2

文字でWebページを作ってみた

document.write("This is the first line \n And this is the second");

望ましい効果は次のとおりです。

This is the first line
And this is the second

しかし、私はこれを取得します:

This is the first line And this is the second

<br>内部に書き込んでみましたが、うまくいきました。しかし、なぜエスケープを書くこと\nができなかったのでしょうか? 私が間違っていることはありますか?私はFirefoxを使用しています。

4

3 に答える 3

5

あなたは何も悪いことをしていません。その\n文字だけが、alert() や などのテキスト関連の要素で有効です<textarea>

このdocument.write()コマンドは、サイトの本文または表示 (HTML) に書き込みます。ページに HTML を記述しているため、hereの<br>代わりに置き換えます\n

于 2013-01-25T09:27:55.357 に答える
4

\nを使用する代わりに<br/>、ブラウザがそれを理解できるようにする必要があります

document.write("This is the first line <br/> And this is the second");
于 2013-01-25T09:27:07.713 に答える
0

や通常の HTML<pre>などにタグを使用します。\n<br/>

http://jsfiddle.net/FxpcA/

document.write("<pre>This is the first line \n And this is the second</pre>");
于 2013-01-25T09:29:19.600 に答える