admin管理员组

文章数量:1398206

In all versions of IE (I'm testing IE11 at the moment) new line breaks inside of textarea elements aren't working...

function id_(id)
{ 
 var r = false;
 if (document.getElementById(id))
 {
  r = document.getElementById(id);
 }
 return r;
}

window.onload = function()
{
 for (i in document.getElementsByTagName('audio')[0])
 {
  id_('test').value = id_('test').value + '\n' +i;
 }
}

How can I force IE to break lines in a textarea element? No frameworks.

I've tried \n\r, \r\n, \r and \n.

In all versions of IE (I'm testing IE11 at the moment) new line breaks inside of textarea elements aren't working...

function id_(id)
{ 
 var r = false;
 if (document.getElementById(id))
 {
  r = document.getElementById(id);
 }
 return r;
}

window.onload = function()
{
 for (i in document.getElementsByTagName('audio')[0])
 {
  id_('test').value = id_('test').value + '\n' +i;
 }
}

How can I force IE to break lines in a textarea element? No frameworks.

I've tried \n\r, \r\n, \r and \n.

Share Improve this question edited Sep 22, 2014 at 17:27 John asked Sep 22, 2014 at 17:17 JohnJohn 13.8k15 gold badges111 silver badges191 bronze badges 4
  • 2 jsfiddle/ff5kqtru this seems to work in IE11. What are u trying to achieve by that loop? – Comfortably Numb Commented Sep 22, 2014 at 17:21
  • Try this $('#test').val($('#test').value + '\n' +i); ref:stackoverflow./questions/5583040/… – Sainath Motlakunta Commented Sep 22, 2014 at 17:24
  • for (i in document.getElementsByTagName('audio')[0]) seems weird, are you sure it isn't for (i in document.getElementsByTagName('audio'))? – Igor Jerosimić Commented Sep 22, 2014 at 17:28
  • 1 @IgorJerosimić, I assume he's iterating through the properties of the first audio tag, hence the [0]. – Rick Hitchcock Commented Sep 22, 2014 at 17:39
Add a ment  | 

3 Answers 3

Reset to default 5

After minimizing the code the issue was the CSS white-space property which I fixed with the code below.

* {white-space: nowrap;}
textarea {white-space: pre;}

In IE, \n does indeed generate line breaks in textarea elements. That is, this code:

<textarea id="target">default text here</textarea>
<script>document.getElementById('target').value = 'a\nb';</script>

does, in IE, result in a textarea which holds the value default text here for an unimaginably short time followed by the value:

a
b

If you really want to test your browser, I have put this on a JSFiddle at: http://jsfiddle/91mj2arh/

This Fiddle based on your code adds line feeds in my version of IE11, and it seems to acplish what you're looking for to iterate the audio element's properties:

http://jsfiddle/ff5kqtru/5/

If you post your HTML, we may be able to spot a problem.

本文标签: javascriptInternet Explorer n line breaks not workingStack Overflow