admin管理员组文章数量:1200345
I am facing the following problem: I have a HTML document where I want to print basic status/debug/etc messages.
So, the HTML document contains an empty pre-element whose id is out:
<body onload='init () && main();'>
<pre id='out'>
</pre>
</body>
</html>
The init() function assigns the pre element to a variable:
var out_div;
function init() {
out_div = document.getElementById('out')
return 1;
}
Then, in order to print something into the pre-div, I use this function:
function txt_out(txt) {
out_div.innerHTML += "\n" + txt
}
The newline should make sure that each invocation of txt_out is written on its own line. This works as expected in Firefox. IE however doesn't seem to pay attention to the \n. So, is this wrong on IE's part, or am I doing something wrong?
I am facing the following problem: I have a HTML document where I want to print basic status/debug/etc messages.
So, the HTML document contains an empty pre-element whose id is out:
<body onload='init () && main();'>
<pre id='out'>
</pre>
</body>
</html>
The init() function assigns the pre element to a variable:
var out_div;
function init() {
out_div = document.getElementById('out')
return 1;
}
Then, in order to print something into the pre-div, I use this function:
function txt_out(txt) {
out_div.innerHTML += "\n" + txt
}
The newline should make sure that each invocation of txt_out is written on its own line. This works as expected in Firefox. IE however doesn't seem to pay attention to the \n. So, is this wrong on IE's part, or am I doing something wrong?
Share Improve this question edited Jul 28, 2014 at 16:58 Ciro Santilli OurBigBook.com 383k117 gold badges1.3k silver badges1.1k bronze badges asked Nov 25, 2009 at 15:53 René NyffeneggerRené Nyffenegger 40.5k34 gold badges176 silver badges315 bronze badges 1 |4 Answers
Reset to default 14This is a known problem in Internet Explorer. As a workaround, insert a <br>
element instead of a newline character.
Doing things like element.innerHtml += something;
is usually a bad practice because it makes the browser re-parse the entire contents of the element. As you add more and more data, every time it gets slower and slower. It also invalidates any references/event listeners that other pieces of code might have on its sub-elements.
Better use a div
tag; something like:
<div id='out'></div>
and then append new sub-elements on the fly:
out_div = document.getElementById('out');
// add a new message
new_element = document.createElement('div');
new_element.textContent = "your message here";
new_element.className = "some_class"; // CSS class for changing the appearance
out_div.appendChild(new_element);
Change your code to:
function txt_out(txt) {
out_div.innerHTML += "<br />" + txt
}
Change to <br />
本文标签: javascriptInsert newline (n) with innerHTML in pre element in IE not workingStack Overflow
版权声明:本文标题:javascript - Insert newline (n) with innerHTML in pre element in IE not working? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738612188a2102694.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.html()
method). – Crescent Fresh Commented Nov 25, 2009 at 16:01