如何在JavaScript警告框中添加新行?
当前回答
JavaScript中的特殊字符代码列表:
Code Outputs
\' single quote
\" double quote
\\ backslash
\n new line
\r carriage return
\t tab
\b backspace
\f form feed
其他回答
适用于\n,但如果脚本是java标记,则必须编写\\\n
<script type="text/javascript">alert('text\ntext');</script>
or
<h:commandButton action="#{XXXXXXX.xxxxxxxxxx}" value="XXXXXXXX"
onclick="alert('text\\\ntext');" />
当你想用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\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
\n将放入新行- \n是新行的控制代码。
警报(“第 1 行\n第 2 行”);
从ECMAScript 2015开始,你可以使用反勾号(' ')来包含多行字符串的模板字面量,如下所示:
警报('第 1 行 第 2 行');
输出:
Line1
Line2