javascript 警报javascript中的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23777679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Newline in alert javascript
提问by Ptheitroad
I want to add a newline in alert message in javascript.
我想在 javascript 中的警报消息中添加一个换行符。
var al = $("#txt_noIncorrect").val().toString();
console.log(al); // al='Incorrect amount!\nFor decimals, use point instead of virgule.';
alert(al);
alert('Incorrect amount!\nFor decimals, use point instead of virgule.')
First alert:
第一个警报:
Second alert:
第二次提醒:
Why first alert does not put the newline?
为什么第一个警报不放换行符?
回答by Phong Vo
You could do this alert("Hello \r\nWorld");
你可以这样做 alert("Hello \r\nWorld");
回答by Arko Elsenaar
You can do this by replacing the \n with a new line character in Javascript using .replace()
您可以通过在 Javascript 中使用换行符替换 \n 来做到这一点 .replace()
alert($("input[type='text']").val().replace(/\\n/g,"\n"));
alert($("input[type='text']").val().replace(/\\n/g,"\n"));
UPDATE
更新
Here is a fiddle example with textarea instead of input text. JSFIDDLE DEMO
这是一个带有 textarea 而不是输入文本的小提琴示例。JSFIDDLE 演示
回答by Atul Arnav Sharma
try this alert("Hello \n World");
试试这个 alert("Hello \n World");