Multiline string in div using javascript

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

I have created a multiline string as follows"

var str1 = document.getElementById('text1'); var str2 = document.getElementById('text2'); var str = str1.value + "\n" + str2.value;

I now want to add this string to a div that I create in a popup window as follows:

myWindow=window.open('','Full_text','width=600,height=600'); myWindow.document.write('<div id="div1" style="position: absolute; left: 10px; width: 580px; top: 30px; height: 550px; overflow: auto;">' + str + '</div>');

But I do not get a multiline string in the div. How do I solve this problem?

回答1:

Line breaks are not rendered in HTML without this CSS property:

white-space: pre;

You can try to replace str to this:

var str = str1.value + "<br/>\n" + str2.value;


回答2:

Line break is not presented when rendering in HTML, if you just need a line break, please use <br> instead of \n If you want to keep white spaces, tabs, you should try to wrap the string with a <pre>string goes here.</pre> tag.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!