Second line")"."/> Second line")".">

Home  >  Article  >  Web Front-end  >  How to break new lines in JavaScript

How to break new lines in JavaScript

coldplay.xixi
coldplay.xixiOriginal
2021-03-12 15:01:3026153browse

How to break lines in js: 1. Use the [\n] line break character, the code is [alert("First line\nSecond line")]; 2. Use the [\r] line break character, The code is [alert("first line\rsecond line")]; 3. Use the HTML [
] tag.

How to break new lines in JavaScript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

Methods for line breaks in JavaScript:

Method 1: Use line breaks

1, \nLine break character

In JavaScript, we can use \n directly where we want to break a line:

<script>
alert("第一行\n第二行");
</script>

2, \r Line break

<script>
alert("第一行\r第二行");
</script>

The above two methods have the same results:

How to break new lines in JavaScript

##Method 2: Use HTML
tag

When you can write content into an HTML document, you can use HTML's
tag to wrap lines.

Example: Use

document.write()

<script>
document.write("第一行<br>第二行")
</script>

Output:

第一行
第二行

Related free learning recommendations:

javascript(video)

The above is the detailed content of How to break new lines in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn