简体   繁体   English

如何在 JS append html 上添加新行

[英]How to add new line on JS append html

I have some node list, and I am trying to get some values from this list.我有一些节点列表,我试图从这个列表中获取一些值。 It works fine but I can't append the values in new lines and everything rendered together.它工作正常,但我不能 append 新行中的值和一起呈现的所有内容。

    <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>

I tried adding <br> on html var but it just prints <br> with ""我尝试在 html var 上添加<br>但它只是用“”打印<br>
\n doesn't work too \n 也不行

Thanks in advance.提前致谢。

EDIT: ok fixed it by编辑:好的修复它

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

append function receive string or HTMLNode element (more info ) append function 接收字符串或 HTMLNode 元素(更多信息

but if your purpose is just to learn,you can simply replace InnerHtml content with your Html;但如果你的目的只是为了学习,你可以简单地用你的 Html 替换 InnerHtml 内容; or concatenate it to the current content;或将其连接到当前内容;

 const indiv = document.querySelector('.newinsert') const flist = document.querySelectorAll('someclass') const listClean = [...flist] console.log(listClean); listClean.forEach(list=> { const html = `${list.innerHTML}<br> ` indiv.innerHTML = html //or indiv.innerHTML = indiv.innerHTML+html // if you effectly want to append conent to current dev content })
 <div class="newinsert"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM