如何使用JSON文件中的数据生成/替换TD标签

时间:2017-02-11 03:19:32

标签: jquery html5

<table id="administration" cellspacing="0" cellpadding="0">
        <thead>
            <tr>
                <th colspan="6" class="caption">Administration</th>
            </tr>
            <tr>
                <th><span>Name</span></th>
                <th><span>Monday</span></th>
                <th><span>Tuesday</span></th>
                <th><span>Wednesday</span></th>
                <th><span>Thursday</span></th>
                <th><span>Friday</span></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="lalign">John Smith<br><span style="color: navy;text-align: center;">Leader</span></td>
                <td>Main Campus AM<br>Main Campus PM</td>
                <td>Main Campus AM<br>Main Campus PM</td>
                <td>Main Campus AM<br>Main Campus PM</td>
                <td>Main Campus AM<br>Main Campus PM</td>
                <td>Main Campus AM<br>Main Campus PM</td>
            </tr>
            <tr>
                <td class="lalign">Vannessa Smith<br><span style="color: navy;text-align: center;">Lead Developer</span></td>
                <td>Main Campus AM<br>Main Campus PM</td>
                <td>Main Campus AM<br>Main Campus PM</td>
                <td>Ex. Campus AM<br>Main Campus PM</td>
                <td>Main Campus AM<br>Main Campus PM</td>
                <td>Main Campus AM<br>Main Campus PM</td>
            </tr>
            <tr>
                <td class="lalign">Jane Phillips<br><span style="color: navy;text-align: center;">Git Officer</span></td>
                <td>Main Campus AM<br>Main Campus PM</td>
                <td>Main Campus AM<br>Main Campus PM</td>
                <td>Main Campus AM<br>Main Campus PM</td>
                <td>Main Campus AM<br>Main Campus PM</td>
                <td>Main Campus AM<br>Main Campus PM</td>
            </tr>
        </tbody>
    </table>

这是我的HARD CODED表

我正在使用包含此人的日程安排的JSON文件。我的问题是从我的文件中替换主校园AM 等条目的最佳/最简单的方法

我还想提一下,我还有5个表,但每个表都有不同的ID(上面这个是管理)

我可以使用此javascript代码

访问每个人的日程安排
v.mondayAM
v.mondayPM
v.tuesdayAM
v.tuesdayPM
v.wednesdayAM
v.wednesdayPM
v.thursdayAM
v.thursdayPM
v.fridayAM
v.fridayAM
v.saturdayAM
v.saturdayPM

1 个答案:

答案 0 :(得分:1)

如果名称是硬编码的,您也可以为<tr>分配一个ID,例如<tr id="leader">

然后您可以更轻松地使用以下内容:

    $("td").filter(":contains('Campus AM')").remove()
    $("#leader").append($("<td>").html(v.mondayAM + '<br>' + v.mondayPM));

如果这是一个对象,你可以在append语句中使用for ... of循环。