如何在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,但如果脚本是java标记,则必须编写\\\n
<script type="text/javascript">alert('text\ntext');</script>
or
<h:commandButton action="#{XXXXXXX.xxxxxxxxxx}" value="XXXXXXXX"
onclick="alert('text\\\ntext');" />
只是为了帮助任何人,当从c#代码后面这样做时,我必须使用双转义字符,否则我会得到一个“未终止字符串常量”JavaScript错误:
ScriptManager.RegisterStartupScript(this, this.GetType(), "scriptName", "alert(\"Line 1.\\n\\nLine 2.\");", true);
Alert ("some text\nmore text in a new line");
alert(“第 1 行\n第 2 行\n第 3 行\n第 4 行\n第 5 行”);
Alert ("some text\nmore text in a new line");
输出:
一些文本 新行中有更多文本
JavaScript中的特殊字符代码列表:
Code Outputs
\' single quote
\" double quote
\\ backslash
\n new line
\r carriage return
\t tab
\b backspace
\f form feed