JavaScript警告框中的换行

439

如何在JavaScript的提示框中插入新行?


1
PHP_EOL 对我有效,因为其他许多答案在此处没有起作用。 - Hayden Thring
20个回答

669

\n 会在文本中换行 - \n 是控制新行的代码。

alert("Line 1\nLine 2");


1
这个在各种浏览器上的兼容性如何? - Ates Goral
4
如果我在 ASP.NET 应用程序的警告消息中使用 \n,对话框甚至不会显示。这是不是 Microsoft 的问题 :) - TheTechGuy
23
给 ASP.NET 后端代码中的鳄鱼猎人,您需要使用转义字符来处理警报。这意味着您需要使用 Registerblabla(bla,bla,"alert('hi\nhi second line')")。 - NicolasT
6
在ASP.NET MVC4中,我使用</br>而不是\n来换行。 - Kevin Meredith
1
在php中,如果我使用以下代码,则不会显示警告窗口: window.alert('Message sent successfully \n It will be reviewed soon')但是当我使用以下代码时,则一切正常: window.alert('Message sent successfully \n It will be reviewed soon') - Ahmed Syed
显示剩余4条评论

52

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

输出:

一些文本
更多文本在新的一行中


44

在 JavaScript 的提示框中,你需要使用双引号来显示特殊字符,例如 \n、\t 等等。比如在 PHP 脚本中:

$string = 'Hello everybody \n this is an alert box';
echo "<script>alert(\"$string\")</script>";

但是当您想要显示双引号括起来的字符串时,可能会出现第二个问题。

参见链接文本

如果字符串被双引号(")包围,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>";

30
在C#中我做了:
alert('Text\\n\\nSome more text');

展示效果如下:

文本

更多文本


20

JavaScript中特殊字符代码列表:

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

19
alert("text\nnew Line Text");

文档: Window.alert()


Firefox:
firefox demo

Chrome:
chrome demo

Edge:
edge demo


9
当你想从php变量中写入javascript alert时,你需要在"\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

为什么这个答案被低估了呢?如果你在编写网页时更加动态地思考,这将会是一个很大的帮助。 - Jimwel Anobong
在 PHP 中使用双引号设置变量时,PHP 会检查双引号内的变量。理论上,这会消耗资源。在 PHP 中创建 js alerttext 的简单直接方法是使用单引号,并在单引号外添加变量,例如 $jsAlert=' bla bla '.$var.' \nmore bla bla'。 - Terradon
我同意,但你不能像你说的那样做。例如,在所有字符串变量来自PHP的情况下,你不能为每个需要换行的字符串编写特定的处理方式。 - Julha

5

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

alert("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");


4

当使用\n时,它可以正常工作。但如果脚本在java标签中,你必须写成\\\n

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

或者

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

在Java标签中,您只需要将其转义两次即可。例如:<%out.print("alert('第一行\n第二行。')");%> - third_eye

4

alert('The transaction has been approved.\nThank you');

只需添加换行符 \n 字符。

alert('The transaction has been approved.\nThank you');
//                                       ^^

这是六年半前Michael Gattuso说的话。他的答案得到了认可。这似乎并没有说出什么新的东西,而基本上只是复制了那个回答。 - Quentin
@Quentin:区别在于单引号和双引号...两种方式都可以使用,这是Michael Gattuso没有提到的。 - Ole Sauffaus
@OleSauffaus - 那是一个无关紧要的差别。 - Quentin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接