最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Multiline string in div using javascript - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked Jul 12, 2012 at 9:46 user828647user828647 5151 gold badge10 silver badges29 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

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;

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.

发布评论

评论列表(0)

  1. 暂无评论