如何在JavaScript警告框中添加新行?
答案 0 :(得分:525)
\n
会在 - \n
中添加一个新行作为新行的控制代码。
alert("Line 1\nLine 2");
答案 1 :(得分:52)
alert("some text\nmore text in a new line");
输出:
some text
more text in a new line
答案 2 :(得分:37)
你必须使用双引号在js警告框中显示像\ n \ t等...的特殊字符 例如php脚本:
$string = 'Hello everybody \n this is an alert box';
echo "<script>alert(\"$string\")</script>";
但是当你想要显示双引号中指定的字符串时,第二个可能的问题就到了。
请参阅link text
如果字符串用双引号(“)括起来,PHP将解释更多特殊字符的转义序列
转义序列\ n转换为0x0A ASCII转义字符,此字符不会显示在警告框中。解决方案是逃避这个特殊的序列:
$s = "Hello everybody \\n this is an alert box";
echo "<script>alert(\"$string\")</script>";
如果您不知道如何包含字符串,则必须将特殊字符转换为其转义序列
$patterns = array("/\\\\/", '/\n/', '/\r/', '/\t/', '/\v/', '/\f/');
$replacements = array('\\\\\\', '\n', '\r', '\t', '\v', '\f');
$string = preg_replace($patterns, $replacements, $string);
echo "<script>alert(\"$string\")</script>";
答案 3 :(得分:28)
在C#中我做了:
alert('Text\\n\\nSome more text');
显示为:
文字
更多文字
答案 4 :(得分:12)
JavaScript中的特殊字符代码列表:
Code Outputs
\' single quote
\" double quote
\\ backslash
\n new line
\r carriage return
\t tab
\b backspace
\f form feed
答案 5 :(得分:9)
alert("text\nnew Line Text");
修改:您不需要额外的引号和字符串
答案 6 :(得分:7)
如果要从php变量写入javascript警报,则必须在“\ n”之前添加其他“\”。 相反,警报弹出窗口无效。
前:
PHP :
$text = "Example Text : \n"
$text2 = "Example Text : \\n"
JS:
window.alert('<?php echo $text; ?>'); // not working
window.alert('<?php echo $text2; ?>'); // is working
答案 7 :(得分:3)
以防这有助于任何人,当从C#代码执行此操作后,我不得不使用双转义字符或者我得到一个“未终止的字符串常量”JavaScript错误:
ScriptManager.RegisterStartupScript(this, this.GetType(), "scriptName", "alert(\"Line 1.\\n\\nLine 2.\");", true);
答案 8 :(得分:3)
与\n
一起使用,但如果脚本是java标记,则必须编写\\\n
<script type="text/javascript">alert('text\ntext');</script>
或
<h:commandButton action="#{XXXXXXX.xxxxxxxxxx}" value="XXXXXXXX"
onclick="alert('text\\\ntext');" />
答案 9 :(得分:3)
alert('The transaction has been approved.\nThank you');
&#13;
只需添加换行符\ n字符。
alert('The transaction has been approved.\nThank you');
// ^^
答案 10 :(得分:2)
alert("some text\nmore text in a new line");
&#13;
alert("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
&#13;
答案 11 :(得分:1)
\n
将不起作用:
<% System.out.print("<script>alert('Some \n text')</script>"); %>
我知道这不是答案,只是认为这很重要。
答案 12 :(得分:1)
使用javascript的新行字符而不是'\ n'.. 例如:“Hello \ nWorld”使用“Hello \ x0AWorld” 它很棒!!
答案 13 :(得分:1)
感谢您的提示。使用“+”符号是我可以使用它的唯一方法。这是添加一些数字的函数的最后一行。我自己就是在学习JavaScript:
alert("Line1: The sum is " + sum + "\n" + "Line 2");
答案 14 :(得分:0)
您可以使用\ n \添加新行
df['Cumulative'] = df.groupby('TransactionId')['Delta'].cumsum()
print (df)
TransactionId Delta Cumulative
0 14 2 2
1 14 3 5
2 14 1 6
3 14 2 8
4 15 4 4
5 15 2 6
6 15 3 9
答案 15 :(得分:0)
您可以使用 \ n 换行
alert("Welcome\nto Jumanji");
alert("Welcome\nto Jumanji");
答案 16 :(得分:0)
从ECMAScript 2015开始,您可以使用反引号(``)将Template Literals括在多行字符串中,如下所示:
alert(`Line1
Line2`);
输出:
Line1
Line2
答案 17 :(得分:-1)
在JAVA应用程序中,如果从“属性”文件中读取了消息。
由于双重编译java-html,
您将需要两次转义。
jsp.msg.1=First Line \\nSecond Line
将导致
First Line
Second Line
答案 18 :(得分:-2)
我用过:“\ n \ r” - 它只适用于双引号。
var fvalue = "foo";
var svalue = "bar";
alert("My first value is: " + fvalue + "\n\rMy second value is: " + svalue);
will alert as:
My first value is: foo
My second value is: bar
答案 19 :(得分:-2)
使用\n
这可以使用
完成alert("first line \n second line \n third line");
输出:
第一行
第二行
第三行
这是为此准备的jsfiddle。