JavaScript 代码用于动态填充时刻表数据(示例):
javascript
// 假设时刻表数据存储在名为timetable 的数组中
const timetable = [{trainNumber: 'G1',fromStation: '北京西站',toStation: '武汉站',departureTime: '07:00',arrivalTime: '10:30'},{trainNumber: 'G5',fromStation: '上海虹桥站',toStation: '武汉站',departureTime: '08:30',arrivalTime: '11:50'},// ...其他时刻表数据
];// 获取时刻表数据表格主体
const tbody = document.querySelector('tbody');// 遍历时刻表数据并创建行
timetable.forEach(train => {const newRow = document.createElement('tr');// 创建并填充列数据const trainNumberCell = document.createElement('td');trainNumberCell.textContent = train.trainNumber;const fromStationCell = document.createElement('td');fromStationCell.textContent = train.fromStation;const toStationCell = document.createElement('td');toStationCell.textContent = train.toStation;const departureTimeCell = document.createElement('td');departureTimeCell.textContent = train.departureTime;const arrivalTimeCell = document.createElement('td');arrivalTimeCell.textContent = train.arrivalTime;// 将列添加到行newRow.appendChild(trainNumberCell);newRow.appendChild(fromStationCell);newRow.appendChild(toStationCell);newRow.appendChild(departureTimeCell);newRow.appendChild(arrivalTimeCell);// 将行添加到表格主体tbody.appendChild(newRow);
});