Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
67 views
in Technique[技术] by (71.8m points)

javascript - Creating new lines in string using jQuery

I have a string that looks this "Hello Name, Choose one of the options below. 1. Phones 2. Computers" When the output is returned, I do not see the newlines taking effect. What could I be missing ?

JS

var message = "Hello Name, Choose one of the options below.

1. Phones

2. Computers"
console.log(messsage);
$('.item').val(message);
question from:https://stackoverflow.com/questions/65905979/creating-new-lines-in-string-using-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Replace the new lines with <br>
The reason is that in HTML new lines are made by the <br> tag

var message = "Hello Name, Choose one of the options below.<br>
1. Phones<br>
2. Computers"
$('.item').html(message);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="item"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...