0
 success: function (data, status) {
            var header = $("#MainDiv");

            header.html(null);
            var headertemplate = $("<table class='searchlistbody'><tr></th><th>Name</th></tr>");
            header.append(headertemplate);

            $.each(data, function () {
                var template = $("<tr> <td class='campnameAltrow'><span class='searchListTitle'></span></td></tr>");

                        template.find('span.searchListTitle').attr('title', this.Title);

                // add the template to the collection
                header.append(template);
            });
            header.append("</table>");
        },

现在,当我试图在表格上设置边框时,我注意到它只在标签上应用边框,即标题(名称)。

 Debugging further in fire bug I saw that the DOM sequence is in this format
        <table class='searchlistbody'><tr><th>Name</th></tr></table>
        <tr> <td class='campnameAltrow'>Test</td></tr>
        <tr> <td class='campnameAltrow'>Test</td></tr>
        <tr> <td class='campnameAltrow'>Test</td></tr>
        <tr> <td class='campnameAltrow'>Test</td></tr>
        <tr> <td class='campnameAltrow'>Test</td></tr>

谁能告诉我为什么在附加所有 for 循环行之前关闭表格......它是一个 AJAX 调用

4

2 回答 2

1

从附加函数的文档来看:http: //api.jquery.com/append/它实际上尝试使用正确的 DOM 元素

不要尝试将其用作字符串连接操作

而是尝试创建表格元素并分别在其中附加 TH 和 TR:

var table = $("<table ...></table>")
table.append("<th>...</th>")
$.each(function() {
  ...
  table.append("<tr>...</tr>")
  ...
})
于 2012-12-17T16:54:54.257 回答
0

您不能将元素添加到 dom 中。因此,您将表格添加到标题中,然后将行附加到表格中

 success: function (data, status) {
            var header = $("#MainDiv");

            header.html(null);
            var headertemplate = $("<table class='searchlistbody'><tr></th><th>Name</th></tr></table>");
            header.append(headertemplate);

            $.each(data, function () {
                var template = $("<tr> <td class='campnameAltrow'><span class='searchListTitle'></span></td></tr>");

                        template.find('span.searchListTitle').attr('title', this.Title);

                // add the template to the collection
                headertemplate.append(template);
            });
        },
于 2012-12-17T16:52:30.850 回答