0

我想在一个 idtable之后显示一个. 的 HTML是硬编码的:h1pageHeadingh1<h1 id="pageHeading">Table</h1>

const pageHeading = document.querySelector("#pageHeading")

该表是使用 Javascript 创建的:

const table = document.createElement("table")
table.setAttribute("id", "table")

我尝试了以下方法:

document.body.appendChild(table)

这将打印table页面上最后一个 HTML 元素之后的但。

然后我尝试了:

tableHeading.appendChild(表)

这将打印table但在h1.

最后,我尝试了:

pageHeading.insertAdjacentHTML(
  "afterend",
  table
)

这根本不打印表格。相反,我得到(在 之后h1):

[object HTMLTableElement]

这可能是因为我.insertAdjacentHTMLtable内容上使用(请参阅下面的完整代码)?

const tableHeaders = [{
  titleOne: "Name",
  titleTwo: "Age",
  titleThree: "Nationality",
}, ]

const persons = [{
    name: "James",
    age: "23",
    nationality: "English",
  },

  {
    name: "Isabella",
    age: "21",
    nationality: "Italian",
  },

  {
    name: "Hank",
    age: "25",
    nationality: "American",
  },

  {
    name: "Manon",
    age: "27",
    nationality: "French",
  },
]
const pageHeading = document.querySelector("#pageHeading")

const table = document.createElement("table")
table.setAttribute("id", "table")
/* document.body.appendChild(table) this puts table AFTER the last item in the body <h2>Test</h2> */
/* tableHeading.appendChild(table) this puts table INSIDE <h1 id="tableHeading">Table</h1> */
pageHeading.insertAdjacentHTML(
  "afterend",
  table
) /* this returns: [object HTMLTableElement] */

const headers = tableHeaders.map(header => {
  let ths = `<tr><th>${header.titleOne}</th><th>${header.titleTwo}</th><th>${header.titleThree}</th></tr>`

  table.insertAdjacentHTML("afterbegin", ths)
})

const personDetails = persons.map(person => {
  let tds = `<tr><td>${person.name}</td><td>${person.age}</td><td>${person.nationality}</td></tr>`

  table.insertAdjacentHTML("beforeEnd", tds)
})
<h1 id="pageHeading">Table</h1>

<h2>Test</h2>

4

1 回答 1

3

insertAdjacentHTML而不是你需要的使用,insertAdjacentElement因为它是一个元素而不是一个 html 字符串,例如:

const tableHeaders = [{
  titleOne: "Name",
  titleTwo: "Age",
  titleThree: "Nationality",
}, ]

const persons = [{
    name: "James",
    age: "23",
    nationality: "English",
  },

  {
    name: "Isabella",
    age: "21",
    nationality: "Italian",
  },

  {
    name: "Hank",
    age: "25",
    nationality: "American",
  },

  {
    name: "Manon",
    age: "27",
    nationality: "French",
  },
]
const pageHeading = document.querySelector("#pageHeading")

const table = document.createElement("table")
table.setAttribute("id", "table")
/* document.body.appendChild(table) this puts table AFTER the last item in the body <h2>Test</h2> */
/* tableHeading.appendChild(table) this puts table INSIDE <h1 id="tableHeading">Table</h1> */

pageHeading.insertAdjacentElement(
  "afterend",
  table
) /* this returns: [object HTMLTableElement] */

const headers = tableHeaders.map(header => {
  let ths = `<tr><th>${header.titleOne}</th><th>${header.titleTwo}</th><th>${header.titleThree}</th></tr>`

  table.insertAdjacentHTML("afterbegin", ths)
})

const personDetails = persons.map(person => {
  let tds = `<tr><td>${person.name}</td><td>${person.age}</td><td>${person.nationality}</td></tr>`

  table.insertAdjacentHTML("beforeEnd", tds)
})
<h1 id="pageHeading">Table</h1>

<h2>Test</h2>

参考:

于 2021-12-01T11:21:33.457 回答