.innerHTML< br>破

.innerHTML< br>破

问题描述:

为什么会这样?我以前没有正确使用.innerHTML,也不知道为什么会这样.

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>";
}

您必须在JavaScript字符串文字中转义换行符:

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>";
}