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


当前回答

当你想用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

其他回答

你可以用\n表示新行

alert("Welcome\nto Jumanji");

警报(“欢迎\ nto Jumanji);

alert("text\nnew Line Text");

文档:Window.alert ()


Firefox:

铬:

优势:

适用于\n,但如果脚本是java标记,则必须编写\\\n

<script type="text/javascript">alert('text\ntext');</script>

or

<h:commandButton action="#{XXXXXXX.xxxxxxxxxx}" value="XXXXXXXX" 
    onclick="alert('text\\\ntext');" />

在c#中,我做到了:

alert('Text\\n\\nSome more text');

它显示为:

文本 更多文本

从ECMAScript 2015开始,你可以使用反勾号(' ')来包含多行字符串的模板字面量,如下所示:

警报('第 1 行 第 2 行');

输出:

Line1
Line2