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


当前回答

\n将不会工作,如果你在Java代码中:

<% System.out.print("<script>alert('Some \n text')</script>"); %>

我知道这不是答案,只是觉得这很重要。

其他回答

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

alert(“第 1 行\n第 2 行\n第 3 行\n第 4 行\n第 5 行”);

当你想用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准备相同。

alert("text\nnew Line Text");

文档:Window.alert ()


Firefox:

铬:

优势:

警报('交易已被批准。\ nThank你);

只需添加一个换行符\n。

alert('The transaction has been approved.\nThank you');
//                                       ^^