JavaScript 中添加 <br> 标签的方法:使用 document.write()使用 innerHTML使用 createElement() 和 appendChild()使用 insertAdjacentHTML()使用模板字符串

如何在 JavaScript 中添加 <br>
<br> 标签在 HTML 中用于在文本中创建换行符。在 JavaScript 中,可以使用以下方法添加 <br>:
1. 使用 document.write()
<code class="js">document.write("<br>");</code>2. 使用 innerHTML
<code class="js">document.getElementById("myElement").innerHTML += "<br>";</code>3. 使用 createElement()
<code class="js">var br = document.createElement("br");
document.body.appendChild(br);</code>4. 使用 insertAdjacentHTML()
<code class="js">document.getElementById("myElement").insertAdjacentHTML("beforeend", "<br>");</code>5. 使用模板字符串
<code class="js">const br = "<br>";
document.getElementById("myElement").innerHTML += br;</code>示例:
以下示例使用 document.write() 方法在文本中添加 <br>:
<code class="html"><html>
<body>
<script>
document.write("Hello");
document.write("<br>");
document.write("World!");
</script>
</body>
</html></code>输出:
<code>Hello World!</code>










