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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:33:06  来源:igfitidea点击:

Newline in alert javascript

javascriptjquerynewlinealert

提问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: enter image description here

第一个警报: 在此处输入图片说明

Second alert: enter image description here

第二次提醒: 在此处输入图片说明

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"));

FIDDLE DEMO

小提琴演示



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");