开发者

Is there a fast way to replace a large tbody in IE?

I need to replace a tbody with new Html using the jQuery below. The .empty() function hangs when the Html is above 1Mb.

$("#searchForm").submit(function (event) {
    event.preventDefault();
    //$("#myTable tbody").empty();
    $.post($(this).attr('action'), $(this).serialize(), function (data) {

        //$("#myTable tbody").html(data);
        replaceTableBody("thebody", data);

    });
});  

I'm not worried if it's a tricky solution, I just want it to work...

I tried this but could not get it to work: replacehtml-for-when-innerhtml-dogs-you-down.

It only needs to work in IE.

EDIT

    var replaceTableBody = function(tbodyId, html){
        var temp  = document.createElement('div');
        temp.innerHTML = '<table><tbody id="thebody">'+html;
开发者_高级运维        var tb = document.getElementById(tbodyId);
        tb.parentNode.replaceChild(temp.firstChild.firstChild, tb);
        temp = null;
    };


i have done this before. you must not use jquery if you want this to be faster.

I wrote a long blog post about this and how its done, its long so i will not paste it here, just link it:

http://notetodogself.blogspot.com/2009/08/javascript-insert-table-rows-table-body.html

The reason you must not use jquery is that jquery does certain things once you pass text into .html(), like search for scripts, execute them, fix certain things etc. this is usually a good thing, but its not a good thing with 1mb of text on IE.


Does it work if your first delete all rows of the tbody with something like $('#myTable tbody').remove() and than call $('#myTable tbody').empty()....if the last step is necessaryat all after removing the rows...


try using the .each() and delete each row seperate.

$("tbody tr").each( function() { $(this).remove(); }); I think the jquery is right, but its the basic idea.


Per the jQuery API documentation:

When .html() is used to set an element's content, any content that was in that element is completely replaced by the new content.

Do you have a need to call empty() prior to updating the tbody?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜