jQuery的附加表中增加了在文本的末尾自动关闭标签。为什么? [英] jQuery appended table adds closing tag at the end of the text automatically. Why?

查看:104
本文介绍了jQuery的附加表中增加了在文本的末尾自动关闭标签。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$('#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>");

输出使用警报得到($('#all_locations)HTML());

<table></table>
<tr><th>City</th></tr>
<tr></tr><td>Seattle</td>
<tr></tr><td>Chicago</td>

这code类火灾时,Ajax调用完成。任何想法为什么会这样?

This code fires when ajax call is finished. Any ideas why is it doing so?

假设数据变量是有效的JSON对象。

Assume that data variable is the valid json object.

推荐答案

尽管jQuery提供,你在元素在DOM,在HTML源代码不标记操作抽象。

Despite the abstraction that jQuery offers, you are operating on elements in the DOM, not tags in the HTML source.

的jQuery('&LT;表&gt;')速记的jQuery(使用document.createElement('表'))

您需要将您的表行追加到表,而不是到容器(同样地,细胞需要被添加到该行)。

You need to append your table rows to the table, not to the container (and likewise, the cells need to be added to the rows).

这篇关于jQuery的附加表中增加了在文本的末尾自动关闭标签。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆