尝试使用Javascript进行换行

ano*_*321 0 javascript line-breaks

尝试使用javascript获得换行符,但它不起作用.

document.getElementById('section').textContent = "Hello world<br>" + msg;
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

document.getElementById('section').textContent = "Hello world" + /n msg;
Run Code Online (Sandbox Code Playgroud)

也不起作用..我错过了什么吗?

Luk*_*uke 5

目前,您正在添加文字文本,而不是HTML <br>元素.你要么想要设置元素的innerHTML ......

document.getElementById('section').innerHTML = "Hello world<br>";
Run Code Online (Sandbox Code Playgroud)

...或创建文本和<br>元素并将其附加到文档中.

var text = document.createTextNode("Hello world"),
    break = document.createElement("br"),
    section = document.getElementById("section");
section.appendChild(text);
section.appendChild(section);
Run Code Online (Sandbox Code Playgroud)