Jquery 动态添加一行数据的问题

yixunchenhua 2012-04-14 03:06:52
前台页面已有代码:

<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 怎么写啊? 对前台不是很了解。 很着急啊。今天加班在弄。。。
...全文
567 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
大雷神 2012-04-16
  • 打赏
  • 举报
回复
if($("#tableClass tr").length==0){ //如果表中没有行
$("#tableClass").append("<tr><td></td></tr>");
}else{
.........
}
yixunchenhua 2012-04-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

$.ajax({ url: "test.html",data:参数, success: function(){

$("#table tr:first-child").before("<tr><td></td></tr>");

}});
[/Quote]

有可能一开始时没数据啊,那就没tr了。。怎么处理?
yixunchenhua 2012-04-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

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

有可能一开始时没数据啊,那就没tr了。。怎么处理?
ie0ie0ie 2012-04-14
  • 打赏
  • 举报
回复
$.ajax({ url: "test.html",data:参数, success: function(){

$("#table tr:first-child").before("<tr><td></td></tr>");

}});
tckoo 2012-04-14
  • 打赏
  • 举报
回复
$("#tableClass tbody tr").before("<tr></tr>")
yixunchenhua 2012-04-14
  • 打赏
  • 举报
回复
怎么判断啊? 可否提供示例代码?
cm_boy 2012-04-14
  • 打赏
  • 举报
回复
做下判断,如果有数据,就insertbefore,否则after
yixunchenhua 2012-04-14
  • 打赏
  • 举报
回复
自己先顶一个!!!!!

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧