javascript .innerHTML <br> 破坏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18990441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.innerHTML <br> breaking
提问by Kevin Banas
Why is this breaking? I've not used .innerHTML correctly before and don't know why this would be wrong.
为什么会这样断?我以前没有正确使用 .innerHTML,不知道为什么会出错。
function asdf() {
document.getElementById("qwerty").innerHTML="A<br>
B<br>
C<br>
D<br>";
}
回答by David says reinstate Monica
You have to escape new-lines in JavaScript string-literals:
您必须在 JavaScript 字符串文字中转义换行符:
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>";
}
回答by SLaks
Javascript string literals cannot contain newlines.
Javascript 字符串文字不能包含换行符。
You can escape the newlines with backslashes:
您可以使用反斜杠转义换行符:
var myString = "a\
b";