前台页面已有代码:
<table width="100%" id="tableClass" border="0" cellspacing="1"
cellpadding="0" class="shr_table">
<thead>
<tr>
<th nowrap="nowrap" width="16%">
报警
</th>
<th width="8%" nowrap="nowrap">
详情
</th>
</tr>
</thead>
<tbody>
<!-- 表格内容 start -->
<c:if test="${not empty list}" var="haveData">
<c:forEach items="${list}" var="alarmrecord" varStatus="status">
<tr>
<td nowrap="nowrap" align="center">
${alarmrecord.alarmd}
</td>
<td nowrap="nowrap" align="center">
${alarmrecord.alarmdetail}
</td>
</tr>
</c:forEach>
</c:if>
</tbody>
</table>需求:ajax 请求一条最新的 报警信息,然后动态添加到 table , 添加为第一行。
注意:此时table中有可能一条数据都还没有。请问这个用 jquery 怎么写啊?  对前台不是很了解。 很着急啊。今天加班在弄

解决方案 »

  1.   

    做下判断,如果有数据,就insertbefore,否则after
      

  2.   

    $("#tableClass tbody tr").before("<tr></tr>")
      

  3.   

    $.ajax({ url: "test.html",data:参数, success: function(){       $("#table tr:first-child").before("<tr><td></td></tr>");}});
      

  4.   

    if($("#tableClass tr").length==0){ //如果表中没有行
    $("#tableClass").append("<tr><td></td></tr>");
    }else{ 
    .........
     }