【问题标题】:Creating tables dynamically in jQuery在 jQuery 中动态创建表
【发布时间】:2010-11-16 15:02:47
【问题描述】:

我正在使用 jQuery 动态构建一些 <table> 数据,但出现以下错误:

未捕获的错误:HIERARCHY_REQUEST_ERR:DOM 异常 3

这发生在脚本的 appendTo 部分,如下所示:

$('<tr />').append(
    /* lots of stuff */
).add(
$('<tr />')
).append(
    /* some more */
).appendTo($tbody);

$tbody$('&lt;tbody /&gt;');

谁能帮帮我?为了完整起见,这是整个代码:

$('#visitsContainer').show();

$div = $('<div />').css('margin-top', '7px').css('width', '620px').addClass('groupBox');
$table = $('<table />').attr('cellpadding', '3').attr('cellspacing', '0').attr('width', '620');
$tbody = $('<tbody />');
$('<tr />').append(
    $('<td />').css('width', '45px').attr('valign', 'top').attr('rowspan', '3').attr('align', 'center').append(
        $('<a />').attr('href', '/sparkx/' + userData.username).append(
                $('<img />').attr('src', '/media/profile/40px/' + userData.photo).attr('alt', userData.firstname).attr('border', '1').css('border-color', '#c0c0c0').css('max-width', ' 42px').css('max-height', ' 40px')
        )
    ).add(
    $('<td />').css('border-bottom', '1px dotted #D21C5B').css('border-right', '1px dotted #D21C5B').css('width', '200px').append(
        $('<a />').attr('href', '/sparkx/' + userData.username).append(
            $('<strong />').text(userData.fullname)
        ).add(
            $('<br />')
        ).add(
            userData.city)
        )
    ).add(
    $('<td />').css('border-bottom', '1px dotted #D21C5B').css('width', '110px').append(
        $('<a />').attr('href', '/profile/' + userData.username + '/sendpm').css('line-height', '18px').append(
            $('<img />').attr('src', '/templates/front/default/images/send_new_icon.gif').attr('alt', 'Stuur bericht').attr('border', '0').attr('align', 'left').css('margin-right', '5px')
        ).append(
            'Stuur bericht')
        )
    ).add(
    $('<td />').css('border-bottom', '1px dotted #D21C5B').css('width', '170px').append(
        $('<b />').text(
            'Geplaatst op:')
        ).append(
            ' ' + posted
        )
    ).add(
    $('<td />').css('border-bottom', '1px dotted #D21C5B').css('width', '135px').append(
        (month > 0 ?
            $('<b />').text('Was hier:')
            :
            $('<div />').css('width', '1px').html('&nbsp;')
        )).append(month > 0 ? ' ' + months[month] + ' ' + year : '')
    )
).add(
    (rating > 0 ?
        $('<tr />').append(
            $('<td />').attr('colspan', '4').append(
                $('<strong />').css('color', '#D21C5B').text(userData.firstname + ' vond dit ').append(
                    (rating == 3 ?
                        $('<i />').text('een aanrader ').add(
                        $('<img />').attr('src', '/templates/front/default/images/thumbGood.png').attr('alt', 'Goed').attr('height', '16').css('margin-left', '3px')
                        )
                    : (rating == 2 ? 
                        $('<i />').text('een aanrader ').add(
                        $('<img />').attr('src', '/templates/front/default/images/thumbAvg.png').attr('alt', 'Redelijk').attr('height', '16').css('margin-left', '3px')
                        )
                    :
                        $('<i />').text('slecht ').add(
                        $('<img />').attr('src', '/templates/front/default/images/thumbBad.png').attr('alt', 'Slecht').attr('height', '16').css('margin-left', '3px')
                        )
                    ))
                )
            )
        )
    : '')
).add(
    (content ?
        $('<tr />').append(
            $('<td />').attr('colspan', '4').append(
                $('<div />').css('width', '100%').text(content).add(
                $('<div />').css('float', 'right').css('clear', 'both').append(
                    $('<a />').attr('href', '/guide/editreaction/' + id).append(
                        $('<b />').text('edit')
                    ).add(
                    $('<a />').attr('href', thisURL + '/rr/' + id).css('padding-left', '10px').append(
                        $('<b />').text('delete')
                    ))
                ))
            )
        )
    : '')
).appendTo($tbody);
$tbody.appendTo($table);

$table.appendTo($div);
$div.prependTo($('#visits'));

【问题讨论】:

  • 值得关注this question 的答案,举一个不太具体的例子。

标签: javascript jquery html-table dom-manipulation


【解决方案1】:

我会认真地重新考虑你在做什么。大量的脚本将变得不可维护并且非常难以调试。你能不能不做所有这些标记创建服务器端并使用 ajax 将它加载到 dom 中。

您目前拥有它的方式也会遇到性能问题,尤其是在您拥有大量数据的情况下。您正在创建多个 jquery dom 对象并进行多个附加。最好是构建一个字符串或推送到一个数组并只附加到 dom 一次。每个追加都会导致重绘,这很昂贵。

为什么不使用专用的dom creation 插件来使您的 js 更具可读性。

另一种选择是查看jTemplates,它允许您在 js 之外定义标记,然后传入要显示的数据。

您也可以考虑使用经过试验和测试的网格插件之一,并有效地为您创建表格结构。谷歌 jqgrid 或 flexigrid。

【讨论】:

  • 感谢您的建议,我会考虑的。但是,这并不能解释为什么我当前的代码不起作用,而且我暂时还是在寻找快速修复。
  • 另外,您的 jTemplates 链接链接到 flexigrid。
  • 没有真正的快速解决方案,因为代码量很难让我了解正在发生的事情。网址或问题的副本会有所帮助。
  • 好的,谢谢。我已经尝试过 jTemplates,似乎效果很好。
猜你喜欢
  • 2011-12-03
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
  • 1970-01-01
相关资源
最近更新 更多