简体   繁体   中英

New line string from javascript

I cant do new line from javascript, I tried to do that with \\n, but nothing.

 document.getElementById("text").textContent = "hhh" + '\\n' + "df";
 <h1 id="text"> abc </h1>

Changing textContent to innerText will do it.

 document.getElementById("text").innerText = "hhh" + '\\n' + "df";
 <h1 id="text"> abc </h1>

The line break behavior is standardized .

Apparently you are pretty new in javascript. You should use <br> instead. \\n new line for console outputs. Also you should use innerHTML instead of textContent .

 document.getElementById("text").innerHTML = "hhh" + '<br>' + "df";
 <h1 id="text"> abc </h1>

With textContent

 document.getElementById("text").textContent = "hhh" + '<br>' + "df";
 <h1 id="text"> abc </h1>

Console example:

 console.log("abc\\ndef");

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