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


当前回答

在JAVA应用程序中,如果消息是从“属性”文件中读取。

由于双重编译java-html,

你需要双重逃生。

jsp.msg.1=First Line \\nSecond Line

会导致

First Line
Second Line

其他回答

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

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

只是为了帮助任何人,当从c#代码后面这样做时,我必须使用双转义字符,否则我会得到一个“未终止字符串常量”JavaScript错误:

ScriptManager.RegisterStartupScript(this, this.GetType(), "scriptName", "alert(\"Line 1.\\n\\nLine 2.\");", true);

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

输出:

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

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

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

or

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

你可以用\n表示新行

alert("Welcome\nto Jumanji");

警报(“欢迎\ nto Jumanji);