问题描述
尽管jQuery提供了抽象,但是您正在操作DOM中的 ,而不是HTML源代码中的标签。
jQuery('<table>')
是的简写jQuery(document.createElement('table'))
。
您需要将表行附加到表,而不是容器(同样,单元格也需要添加到行)。
解决方法
$('#all_locations').append("<table>");
$('#all_locations').append("<tr><th>City</th></tr>");
$.each(data,function(i,item){
$('#all_locations').append("<tr>");
$('#all_locations').append("<td>"+item.city+"</td>");
$('#all_locations').append("<tr>");
}
$('#all_locations').append("</table>");
使用输出 alert($('#all_locations').html());
<table></table>
<tr><th>City</th></tr>
<tr></tr><td>Seattle</td>
<tr></tr><td>Chicago</td>
当ajax调用完成时,此代码将触发。任何想法为什么这样做?
假设数据变量是有效的JSON对象。