使用 JavaScript 动态生成表格 - 排序电影数据
To insert the sorted movie data into the table, you can use JavaScript to dynamically create and append table rows and cells based on the sorted movie array.
Here's an example of how you can do it:
Add the following code inside the script tag:
// Get the table body element
var tableBody = document.getElementById("tb");
// Iterate through the sorted movie array
move1.forEach(function (value, index) {
// Create a new row element
var row = document.createElement("tr");
// Create three cell elements for rank, movie, and box office
var rankCell = document.createElement("td");
var movieCell = document.createElement("td");
var boxOfficeCell = document.createElement("td");
// Set the text content of the cells
rankCell.textContent = index + 1;
movieCell.textContent = value[0];
boxOfficeCell.textContent = value[1];
// Append the cells to the row
row.appendChild(rankCell);
row.appendChild(movieCell);
row.appendChild(boxOfficeCell);
// Append the row to the table body
tableBody.appendChild(row);
});
This code will dynamically create a new table row for each movie in the sorted array and append it to the table body. Each row will contain three cells for the rank, movie name, and box office value.
Make sure to place this code after the sorting logic (before the closing script tag) in order to correctly populate the table with the sorted data.
AI 优化版
原文地址:https://www.cveoy.top/t/topic/qofG 著作权归作者所有。请勿转载和采集!