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
  • 1 Yes it's wrong on IE's part. IE strips leading whitespace when setting innerHTML: bytes.inso.cc/wp/2009/11/07/… Libraries can help alleviate this (jQuery I know does with it's .html() method). – Crescent Fresh Commented Nov 25, 2009 at 16:01
Add a comment  | 

4 Answers 4

Reset to default 14

This 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