用javascript附加一个表

append a table in javascript

本文关键字:一个 javascript      更新时间:2023-09-26

我有一个表,每当用户单击开始事件按钮时都会显示数据,但当用户再次单击开始按钮时,我需要知道如何附加记录,上一条记录会被覆盖

这是我的javascript代码

function DisplayData(downTime) {
        console.log(downTime);
        var newContent = '';
        $.each(downTime.data, function (i, item) {
            newContent += Hesto.Html.StartTR(item.downTime);     
            newContent += Hesto.Html.CreateTD(item.CategoryName);
            newContent += Hesto.Html.CreateTD(item.StartTime);
            newContent += Hesto.Html.CreateTD(item.EndTime);
            newContent += Hesto.Html.CreateTD(item.Comments);
            newContent  = Hesto.Html.EndTR(newContent);
        });
        $('#DowntimeList').html(newContent);
 }

这是我的html代码:

<table id="Downtimetable" class="hesto">
            <thead>
                 <tr>
                     <th>End Of DownTime</th>                   
                     <th>Category Name</th>
                     <th>Start Time</th>
                     <th>End Time</th>
                     <th>Comments</th>
                 </tr>
             </thead>
             <tbody id="DowntimeList">
             </tbody>
             <tfoot>
             </tfoot>
        </table>

使用append()而不是覆盖html():

$('#DowntimeList').append(newContent);

正如您在问题标题中所建议的,append()it.

$('#DowntimeList').html(newContent);

应该是

$('#DowntimeList').append(newContent);

简单使用,

 document.getElementById('DowntimeList').InnerHtml = document.getElementById('DowntimeList').InnerHtml+newContent