简体   繁体   中英

How can i start a new line in javascript

document.getElementById("print").innerHTML = good; 

I want to start writing on new line when using this function for the second time

Here is what I would recommend:

  1. Define the element once at the beginning to save getting it every time.
  2. Create a custom function called print() which you can call more easily.
  3. Use += to add and not overwrite content.
  4. Use br elements to add line breaks after each printed message.

 printDiv = document.getElementById("print"); function print(text) { printDiv.innerHTML += text + '<br>'; } print('This text'); print('is just here'); print('to test the function');
 <div id="print"></div>

You are generating HTML.

A new line in HTML is usually created with a <br> element .

That might not be the right option for the data you are generating though. HTML is a semantic markup language so you should generate the markup that describes the semantics of the data you are working with.

That might involve creating a list with list items in it, multiple paragraphs, or something else.

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