Home > Article > Web Front-end > How to wrap prompt in js
When using the prompt() method in JavaScript, you can achieve line breaks through the following three methods: 1. Insert the "\n" character at the position where you want to break the line; 2. Use the line break character in the prompt text; 3 . Use CSS "white-space: pre" style to force line breaks.
How to use the prompt() method in JavaScript for line breaks
prompt() in JavaScript
method provides a way to get user input, but it does not wrap the input by default.
To make the prompt()
method support line breaks, you can use the following method:
Use the \n character:
<code>var input = prompt("请输入多行文本\n第一行:\n第二行:");</code>
Using multi-line prompt text:
<code>var input = prompt(" 请输入多行文本: 第一行: 第二行:");</code>
Use CSS style:
<code><!DOCTYPE html> <html> <head> <style> .prompt-dialog { white-space: pre; } </style> </head> <body> <script> var input = prompt("请输入多行文本(使用 CSS 样式换行):"); </script> </body> </html></code>
The above is the detailed content of How to wrap prompt in js. For more information, please follow other related articles on the PHP Chinese website!