You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JavaScript中通过append添加HTML时实现换行

问题:节点列表内容追加后无法换行

我有一个节点列表,尝试从中获取值,功能运行正常,但追加的内容无法换行,所有内容都被渲染在一起。相关代码如下:

<div class="newinsert"></div>

<script>
  const indiv = document.querySelector('.newinsert')
  const flist = document.querySelectorAll('someclass')
  const listClean = [...flist]
  
  console.log(listClean);
  listClean.forEach(list=> {
    const html = `${list.innerHTML} `
    indiv.append(html)
  })
</script>

我尝试在html变量中添加<br>,但它只是原样输出带引号的<br>;使用\n也无效。


编辑:已通过以下方式修复:

indiv.insertAdjacentHTML('beforeend', `${html} <br>`)

内容的提问来源于stack exchange,提问作者webmar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 13:40:08