How to wrap the alert box in JavaScript: use \n escape character: const myString = "First line\nSecond line\nThird line"; alert(myString);Use HTML
In JavaScript, you can use \n
Escape characters wrap in alert boxes.
Here's how to do it:
<code class="javascript">// 创建一个字符串,其中包含多个换行符 const myString = "第一行\n第二行\n第三行"; // 使用 `alert()` 函数显示字符串 alert(myString);</code>
Result:
##Other ways to wrap linesBesides using\ n In addition to escaping characters, there are other ways to wrap lines in alert boxes:
HTML Tags:
tags can be included in the string to force newlines.
<code class="javascript">const myString = "第一行<br>第二行<br>第三行"; alert(myString);</code>
CSS white-space Attribute:
You can set the white-space attribute of a string to
pre-line, which preserves line breaks and wraps automatically.
<code class="javascript">const myString = ` 第一行 第二行 第三行 `; alert(myString);</code>
The above is the detailed content of How to wrap alert in javascript. For more information, please follow other related articles on the PHP Chinese website!