-3

私は次のhtmlテーブルを持っています:

 <table>
   <tr>
     <th>  </th>
     <th> Monday </th> 
     <th> Tuesday </th> 
     <th> Wednesday</th> 
     <th> Thursday </th> 
     <th> Friday </th>
     <th> Saturday</th> 
   </tr>

    <tr>
      <td> 8:30 - 10:00 </td>
      <td> Math</td>
      <td>Math </td>
      <td>Math</td>
      <td> Math </td>
      <td> Math</td>
      <td> Math</td>
    </tr>
</table>

JavaScriptのみを使用してそのようなテーブルを作成するにはどうすればよいですか?

4

1 に答える 1

0
<div id="output"></div>
<script>
    var days = {
        "Monday": ["8:30 - 10:00", "Math", "Math", "Math", "Math", "Math", "Math"],
        "Tuesday": [],
        "Wednesday": [],
        "Thursday ": [],
        "Friday": [],
        "Saturday": []

    }

    var html;

    html = "<table><tr>"
    for (var day in days) {

        html += "<th>" + day + "</th>";

    }

    html += "</tr>"
    for (var day in days) {

        if (days[day].length != 0) {
            html += "<tr>"

            for (var i = 0; i < days[day].length; i++) {
                html += "<td>" + days[day][i] + "</td>"
            }
        }

         html += "</tr>";
    }
    html += "</table>";

    document.getElementById("output").innerHTML = html;
</script>
于 2012-12-23T20:07:26.753 に答える