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>
以上就是js如何添加<br>的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号