admin管理员组

文章数量:1325236

I have some content like this

var p =  

 I myself
 Abhimanyu Singh
 Yadav

when I m trying to insert into as innerHTML to some div the whole content appears in one line. I'm using <pre> tag to avoid this problem. But need some appropriate solution.

I have some content like this

var p =  

 I myself
 Abhimanyu Singh
 Yadav

when I m trying to insert into as innerHTML to some div the whole content appears in one line. I'm using <pre> tag to avoid this problem. But need some appropriate solution.

Share Improve this question edited Sep 30, 2020 at 21:45 Alex 1,5801 gold badge15 silver badges27 bronze badges asked Aug 10, 2009 at 5:44 AbhimanyuAbhimanyu 4,9177 gold badges35 silver badges45 bronze badges 3
  • Can I understand your question as "When inserting multi-line text via Javascript it displays in one line. How can I insert <br> tags instead?" – deceze Commented Aug 10, 2009 at 5:50
  • Can you please post the code for this? – rahul Commented Aug 10, 2009 at 5:50
  • I don't think its related to question formatting. – rahul Commented Aug 10, 2009 at 5:52
Add a ment  | 

2 Answers 2

Reset to default 5

You can replace the new line characters with html BR elements (<br />):

document.getElementById('myDiv').innerHTML = p.replace(/\n/g, '<br />');
var p = "i myself<br />Abhimanyu Singh<br />Yadav";

updated to make andrew happy. a still non-semantic answer that is more semantic.

本文标签: javascriptHTML ltbrgt and innerHTMLStack Overflow