<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>电影票房排行榜</title>
    <style>
        table,tr,td{
            border: 1px solid black;
            border-collapse: collapse;
        }
        table{
            width: 400px;
            text-align: center;
        }
    </style>
</head>
<body>
<table>
    <tr>
        <td>排名</td>
        <td>影片</td>
        <td>票房</td>
    </tr>
    <tbody id="tb"></tbody>
</table>
<script>
    //电影数组
    var movieArr =['隐秘而伟大','八佰','疯狂动物城','头师傅一体'];
    // 电影票房数组
    var boxofficeArr = [14.7,33.9,12,15.3];
<pre><code>//创建一个二维数组存放电影名跟票房
var move=[];
for (var i=0;i&lt;movieArr.length;i++){
    move.push([movieArr[i],boxofficeArr[i]])
}
console.log(move)
</code></pre>
<p>//排序
function two(a,b){
return a[1]-b[1];
}
var move1=move.sort(two);
//console.log(move1)
//遍历数组
/* move1.forEach(function (value, index){
console.log(index+1+value)
});*/</p>
<pre><code>// Get the table body element
var tableBody = document.getElementById(&quot;tb&quot;);

// Loop through the sorted movie array
for (var i = 0; i &lt; move1.length; i++) {
  // Create a new table row
  var row = document.createElement(&quot;tr&quot;);

  // Create table cells for rank, movie name, and box office
  var rankCell = document.createElement(&quot;td&quot;);
  var movieCell = document.createElement(&quot;td&quot;);
  var boxOfficeCell = document.createElement(&quot;td&quot;);

  // Set the text content of the cells
  rankCell.textContent = i + 1;
  movieCell.textContent = move1[i][0];
  boxOfficeCell.textContent = move1[i][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);
}
</code></pre>
</script>
</body>
</html>
电影票房排行榜 - 查看最新电影票房排名