admin管理员组

文章数量:1244247

Why is this breaking? I've not used .innerHTML correctly before and don't know why this would be wrong.

function asdf() {
    document.getElementById("qwerty").innerHTML="A<br>
      B<br>
      C<br>
      D<br>";
}

Why is this breaking? I've not used .innerHTML correctly before and don't know why this would be wrong.

function asdf() {
    document.getElementById("qwerty").innerHTML="A<br>
      B<br>
      C<br>
      D<br>";
}
Share Improve this question asked Sep 24, 2013 at 19:32 Kevin BanasKevin Banas 3112 gold badges4 silver badges13 bronze badges 3
  • 1 what do you mean by "breaking"? – iamkrillin Commented Sep 24, 2013 at 19:33
  • 1 Hope the problem is not because of \ !! – Rahul Tripathi Commented Sep 24, 2013 at 19:34
  • 1 stackoverflow./questions/805107/… – u8sand Commented Sep 24, 2013 at 19:34
Add a ment  | 

2 Answers 2

Reset to default 6

You have to escape new-lines in JavaScript string-literals:

function asdf() {
    document.getElementById("qwerty").innerHTML="A<br>\
      B<br>\
      C<br>\
      D<br>";
}

Though you could, potentially more-easily, simply insert newlines in the string itself:

function asdf() {
    document.getElementById("qwerty").innerHTML = "A<br>\nB<br>\nC<br>\nD<br>";
}

Javascript string literals cannot contain newlines.

You can escape the newlines with backslashes:

var myString = "a\
b";

本文标签: javascriptinnerHTML ltbrgt breakingStack Overflow