实时更新,精彩不容错过
<script>
// 定义一个函数来刷新比分数据function refreshScores() {// 使用 Fetch API 获取比分数据fetch("api/scores").then(response => response.json()).then(data => {// 清空表格中的数据document.getElementById("scores").tBodies[0].innerHTML = "";// 遍历比分数据并动态创建表格行data.forEach(match => {let row = document.createElement("tr");let timeCell = document.createElement("td");timeCell.innerText = match.time;let homeTeamCell = document.createElement("td");homeTeamCell.innerText = match.homeTeam;let awayTeamCell = document.createElement("td");awayTeamCell.innerText = match.awayTeam;let scoreCell = document.createElement("td");scoreCell.innerText = match.score;let statusCell = document.createElement("td");statusCell.innerText = match.status;if (match.status === "进行中") {statusCell.classList.add("live");} else if (match.status === "已结束") {statusCell.classList.add("finished");}row.appendChild(timeCell);row.appendChild(homeTeamCell);row.appendChild(awayTeamCell);row.appendChild(scoreCell);row.appendChild(statusCell);document.getElementById("scores").tBodies[0].appendChild(row);});});}// 每隔 10 秒钟刷新一次比分数据setInterval(refreshScores, 10000);
</script>
相关阅读: 即时追踪精彩对决 足球比分一网打尽