简体   繁体   中英

How to display multi-line text on a single line

I get a string from innerHtml . The string is on two lines, as you can see from the image I attached below:

在此输入图像描述

What I'm trying to do is to display it in one line. I've tried this, but it did not work:

var dataToAdd = v.innerHTML.replace("\r\n", "" );

Changing it to a regex with global modifier to do it for multiple lines and to separate the newline and carriage return. Your replace function searches for both characters after another and replace that, but often you wont have a carriage return.

 var dataToAdd = document.getElementById("test").innerHTML.replace(/[\\n\\r]/g, "" ); console.log(dataToAdd); 
 <div id="test">1 2 3 </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM