jQuery附加表会自动在文本末尾添加结束标记为什么?

问题描述

尽管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对象。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...