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


当前回答

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

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

其他回答

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

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

or

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

\n将放入新行- \n是新行的控制代码。

警报(“第 1 行\n第 2 行”);

我看到有些人在MVC中遇到了问题,所以……使用模型传递“\n”的一个简单方法是使用HTML,在我的情况下甚至使用翻译文本。Raw来插入文本。这为我解决了问题。在下面的代码中,Model。警报可以包含换行符,如“Hello\nWorld”…

alert("@Html.Raw(Model.Alert)");

当你想用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中的特殊字符代码列表:

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