如何在JavaScript警告框中添加新行?


当前回答

javascript中的新行字符可以通过使用\n实现

这可以用

alert("first line \n second line \n third line");

输出:

第一行 第二行 第三行

这里有一个jsfiddle准备相同。

其他回答

当你想用javascript写一个php变量的alert时,你必须在“\n”之前加一个“\”。 相反,弹出的警告不起作用。

ex:

PHP :
$text = "Example Text : \n"
$text2 = "Example Text : \\n"

JS:
window.alert('<?php echo $text; ?>');  // not working
window.alert('<?php echo $text2; ?>');  // is working

javascript中的新行字符可以通过使用\n实现

这可以用

alert("first line \n second line \n third line");

输出:

第一行 第二行 第三行

这里有一个jsfiddle准备相同。

JavaScript中的特殊字符代码列表:

Code    Outputs
\'  single quote
\"  double quote
\\  backslash
\n  new line
\r  carriage return
\t  tab
\b  backspace
\f  form feed

谢谢你的提示。使用“+”号是我能让它工作的唯一方法。这是加法函数的最后一行。我自己也在学习JavaScript:

alert("Line1: The sum is  " + sum + "\n" + "Line 2");

Alert ("some text\nmore text in a new line");

输出:

一些文本 新行中有更多文本