简体   繁体   English

如何在渲染时提高性能 <table> 在IE 8?

[英]How to improve performance when rendering <table> in IE 8?

I have a jquery function which adds tag to first row of table. 我有一个jquery函数,它将标记添加到表的第一行。 I tried using append, however its not working, so i got a solution which is very slow and it somehow gives error "Script on this page is causing internet explorer run slow..." 我尝试使用append,但它不起作用,所以我得到了一个非常慢的解决方案,它以某种方式给出错误“此页面上的脚本导致Internet Explorer运行缓慢...”

Function is as 功能如

 jQuery.fn.fixGridView = function () {
        "use strict";
       // var start = +new Date();  // log start timestamp 
        if (jQuery(this).is('table') && this.find('thead').length === 0) {

            var theadv = "<thead><tr>" + this.find('tbody tr:first').html(); +"</tr></thead>";

            this.find('tbody tr:first').remove();
            var htmlv = this.html();
            this.html(theadv + htmlv);
        }
        //var end = +new Date();  // log end timestamp
       // var diff = end - start;
       // alert(diff);
        return this;
    };

Can anybody help me to make this code run faster? 任何人都可以帮助我让这段代码运行得更快吗?

EDIT: I have to use IE..that is the requirement (ie8). 编辑:我必须使用IE ..这是要求(ie8)。 Edit2: I have created js fiddle: http://jsfiddle.net/4xLzL/ Edit2:我创建了js小提琴: http//jsfiddle.net/4xLzL/

To increase rendering performance you must understand that DOM manipulation (including reflows & repaints) are expensive operations. 要提高渲染性能,您必须了解DOM操作(包括回流和重新绘制)是昂贵的操作。 Your code currently re-creates the entire table with the <thead> added the majority of the <tbody> content remains the same. 您的代码当前重新创建了整个表,其中<thead>添加了大部分<tbody>内容保持不变。 This massive "redraw" of the table is highly inefficient. 这种大规模的“重绘”表非常低效。 Especially when in IE 8, where rendering tables is extra slow, you have to modify the DOM as little as possible. 特别是在IE 8中,渲染表格太慢,你必须尽可能少地修改DOM。

I've refactored your logic to minimize the number of lookups performed to find elements by saving them to a variable to be re-used. 我重构了你的逻辑,通过将元素保存到一个可以重用的变量来最小化查找元素所执行的查找次数。 Also, removed the .html('...') call that re-renders the table, but instead used the .prepend() function to add the <thead> into the <table> as the first child. 此外,删除了重新呈现表的.html('...')调用,而是使用.prepend()函数将<thead> <table>作为第一个子项添加到<table>中。

jQuery.fn.fixGridView = function () {
    "use strict";
    var start = +new Date(); // log start timestamp 
    if (this.is('table') && this.children('thead').length === 0) {

        var firstRow = this.children('tbody').children('tr:first');
        var thead = "<thead><tr>" + firstRow.html() + "</tr></thead>";
        firstRow.remove();
        this.prepend(thead);
    }
    var end = +new Date(); // log end timestamp
    var diff = end - start;
    alert(diff);
    return this;
};

$(document).ready(function () {
    $('table[id*="gvCategories"]').fixGridView();
});

Go ahead and test it in IE8: http://jsfiddle.net/amyamy86/4xLzL/7/ 继续在IE8中测试它: http//jsfiddle.net/amyamy86/4xLzL/7/

The problem is not with the plugin, but with your selector. 问题不在于插件,而在于您的选择器。 You only want tables, so modify your selector to be as follows. 您只需要表,因此请将选择器修改为如下所示。

$('table [id*="gvCategories"]').fixGridView();

I also updated the fiddle . 我也更新了小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM