首页 > 解决方案 > 附加元素时获取行为?

问题描述

为什么appendChild正常工作?console.log 显示了一个空的 tr 元素,我认为这是因为 fetch(在 api_search 函数中)是异步的,但是 appendChild 设法用获取的数据填充我的表,所以那里的行有什么不同反对console.log?

let row = document.createElement("tr");
if (ref.record) {
  api_search(ref.record.$ref, {
      fields: ['titles', 'authors', 'citation_count']
    })
    .then(data => {
      var title = data.titles[0].title
      var url = api_to_url(ref.record.$ref)
      var authors = data.authors
      var citations = data.citation_count

      row.innerHTML = inspire_paper_info(title, authors, url, citations)
    })
  console.log(row)
  inspireTable.appendChild(row)
}

标签: javascriptdomfetch-api

解决方案


推荐阅读