Anonymous
Почему мой код JavaScript не добавляет строки в таблицу HTML на загрузке страницы? [закрыто]
Сообщение
Anonymous » 28 май 2025, 11:35
Почему это не работает?
Код: Выделить всё
document.addEventListener("DOMContentLoaded",() => {
const students = data.map(student => createStudent(student))
students.forEach(el => document.getElementById("tartalom").appendChild(el))
})
function createStudent(student){
const tr = document.createElement("tr")
const nevTd = document.createElement("td")
nevTd.textContent = student.nev
tr.appendChild(nevTd)
const osztalyTd = document.createElement("td")
osztalyTd.textContent = student.osztaly
tr.appendChild(osztalyTd)
const korTd = document.createElement("td")
korTd.textContent = student.kor
tr.appendChild(korTd)
const torlesTd = document.createElement("td")
torlesTd.innerHTML = "Törlés"
tr.appendChild(torlesTd)
return tr
}
const data = [
{ "id": 1, "nev": "Kovács Anna", "osztaly": "10.A", "kor": 16 },
{ "id": 2, "nev": "Nagy Péter", "osztaly": "11.B", "kor": 17 },
{ "id": 3, "nev": "Szabó Eszter", "osztaly": "9.C", "kor": 15 },
{ "id": 4, "nev": "Varga Dániel", "osztaly": "12.A", "kor": 18 },
]< /code>
Подробнее здесь:
https://stackoverflow.com/questions/796 ... -page-load
1748421335
Anonymous
Почему это не работает?[code]document.addEventListener("DOMContentLoaded",() => { const students = data.map(student => createStudent(student)) students.forEach(el => document.getElementById("tartalom").appendChild(el)) }) function createStudent(student){ const tr = document.createElement("tr") const nevTd = document.createElement("td") nevTd.textContent = student.nev tr.appendChild(nevTd) const osztalyTd = document.createElement("td") osztalyTd.textContent = student.osztaly tr.appendChild(osztalyTd) const korTd = document.createElement("td") korTd.textContent = student.kor tr.appendChild(korTd) const torlesTd = document.createElement("td") torlesTd.innerHTML = "Törlés" tr.appendChild(torlesTd) return tr } const data = [ { "id": 1, "nev": "Kovács Anna", "osztaly": "10.A", "kor": 16 }, { "id": 2, "nev": "Nagy Péter", "osztaly": "11.B", "kor": 17 }, { "id": 3, "nev": "Szabó Eszter", "osztaly": "9.C", "kor": 15 }, { "id": 4, "nev": "Varga Dániel", "osztaly": "12.A", "kor": 18 }, ]< /code> [/code] Подробнее здесь: [url]https://stackoverflow.com/questions/79641814/why-doesnt-my-javascript-code-append-rows-to-the-html-table-on-page-load[/url]