it-source

JavaScript 알림 상자의 새 줄

criticalcode 2022. 12. 9. 21:52
반응형

JavaScript 알림 상자의 새 줄

JavaScript 경보 상자에 새 행을 삽입하려면 어떻게 해야 합니까?

\n새로운 라인을 추가한다.\n새 라인의 제어 코드가 됩니다.

alert("Line 1\nLine 2");

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

출력:

some text
more text in a new line

\n \t 등 특수 문자를 표시하려면 큰따옴표를 사용해야 합니다.js 경보 상자에 php 스크립트의 example:

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

C#에서는 다음 작업을 수행했습니다.

alert('Text\\n\\nSome more text');

다음과 같이 표시됩니다.

본문

텍스트 추가

JavaScript의 특수 문자 코드 목록:

Code    Outputs
\'  single quote
\"  double quote
\\  backslash
\n  new line
\r  carriage return
\t  tab
\b  backspace
\f  form feed
alert("text\nnew Line Text");

문서:


파이어폭스:
파이어폭스 데모

크롬:
크롬 데모

가장자리:
에지 데모

php 변수에서 javascript 경보를 쓰려면 "\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

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

새 행 \n 문자를 추가합니다.

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

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

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

이것이 도움이 될 경우를 대비해서 뒤에 있는 C# 코드에서 이 작업을 수행할 때 이중 이스케이프 문자를 사용하거나 "unterminated string constant" JavaScript 오류를 표시해야 했습니다.

ScriptManager.RegisterStartupScript(this, this.GetType(), "scriptName", "alert(\"Line 1.\\n\\nLine 2.\");", true);

사용 가능\n그러나 스크립트가 자바 태그에 들어가 있다면 당신은 반드시 써야 한다.\\\n

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

또는

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

ECMAScript 2015 에서는 다음과 같은 여러 줄 문자열의 템플릿 리터럴을 백틱(' ')으로 묶을 수 있습니다.

alert(`Line1
Line2`);

출력:

Line1
Line2

javascript의 새 행 문자를 '\n' 대신 사용합니다.예: "Hello\nWorld"는 "Hello\x0"을 사용합니다.대박이다!!

새 줄에 \n을 사용할 수 있습니다.

alert("Welcome\nto Jumanji");

alert("Welcome\nto Jumanji");

힌트 고마워요."+" 기호를 사용하는 것만이 작동시킬 수고를 덜 수 있습니다.이것은 몇 개의 숫자를 추가하는 함수의 마지막 줄입니다.저는 JavaScript를 배우고 있습니다.

alert("Line1: The sum is  " + sum + "\n" + "Line 2");

\nJava 코드 안에 있으면 작동하지 않습니다.

<% System.out.print("<script>alert('Some \n text')</script>"); %>

답이 아닌 거 알아, 그냥 중요하다고 생각했을 뿐이야.

MVC에서 이 문제로 고생하는 사람들이 있어서...모델을 사용하여 '\n'을 전달하는 간단한 방법은 번역된 텍스트를 사용하더라도 HTML.Raw를 사용하여 텍스트를 삽입하는 것입니다.그걸로 해결됐어요.아래 코드는 모델입니다.경고에 "Hello\nWorld"와 같은 새 행이 포함될 수 있습니다.

alert("@Html.Raw(Model.Alert)");

JAVA 앱에서 "properties" 파일에서 메시지를 읽는 경우.

이중 컴파일 java-html로 인해

이중 탈출이 필요할 거야

jsp.msg.1=First Line \\nSecond Line

결과적으로

First Line
Second Line

행는 javascript를 수 .\n

이것은, 다음의 방법으로 실시할 수 있습니다.

alert("first line \n second line \n third line");

출력:

제1행

두 번째 줄

삼행

여기 같은 것을 위해 준비된 jsfiddle이 있습니다.

사용: "\n\r" - 큰따옴표로 묶어서만 사용할 수 있습니다.

var fvalue = "foo";
var svalue = "bar";
alert("My first value is: " + fvalue + "\n\rMy second value is: " + svalue);

will alert as:

My first value is: foo
My second value is: bar

언급URL : https://stackoverflow.com/questions/1841452/new-line-in-javascript-alert-box

반응형