简体   繁体   English

从$(document).ready调用的jQuery函数花费的时间太长-IE弹出“停止运行脚本”对话框

[英]Jquery function called from $(document).ready takes too long - IE pops up “stop running script” dialog

I have the following script to mimic "freezepane" functionality in html, like in excel, where the right side and the header are both scrolled when the user scrolls the results table. 我有以下脚本来模仿html中的“ freezepane”功能,例如在excel中,当用户滚动结果表时,右侧和标头都将滚动。

fnAdjustTable=function(){

    var colCount=$('#firstTr>td').length; //get total number of column

    var m=0;
    var n=0;
    var brow='mozilla';
    jQuery.each(jQuery.browser, function(i, val) {
        if(val==true){

            brow=i.toString();
        }
    });
    $('.tableHeader').each(function(i){
        if(m<colCount){

            if(brow=='mozilla'){
                $('#firstTd').css("width",$('.tableFirstCol').innerWidth());//for adjusting first td

                $(this).css('width',$('#table_div td:eq('+m+')').innerWidth());//for assigning width to table Header div
            }
            else if(brow=='msie'){
                $('#firstTd').css("width",$('.tableFirstCol').width());

                $(this).css('width',$('#table_div td:eq('+m+')').width());//In IE there is difference of 2 px
            }
        }
        m++;
    });

    $('.tableFirstCol').each(function(i){
        if(brow=='mozilla'){
            $(this).css('height',$('#table_div td:eq('+colCount*n+')').outerHeight()-1);//for providing height using scrollable table column height
        }else if(brow=='msie'){

            $(this).css('height',$('#table_div td:eq('+colCount*n+')').innerHeight());
        }else{
            $(this).css('height',$('#table_div td:eq('+colCount*n+')').height());
        }
        n++;
    });



}

fnScroll=function(){

    $('#divHeader').scrollLeft($('#table_div').scrollLeft());
    $('#firstcol').scrollTop($('#table_div').scrollTop());
}

The problem is that when iterating through the "tableFirstCol" tds, the error to stop running the script pops up. 问题是,当遍历“ tableFirstCol” tds时,弹出停止运行脚本的错误。 Is there some more efficient way to do this? 有没有更有效的方法来做到这一点?

Essentially the script is resizing each top header and side pane to match the width in the first row/column. 本质上,脚本将调整每个顶部标题和侧窗格的大小,以匹配第一行/列的宽度。 If I run my report with a large date range (the side header), the script pops up, usually when there are about more than 30 side header rows. 如果我以较大的日期范围(侧面标题)运行报表,则通常会在大约30多个侧面标题行时弹出脚本。

Any help appreciated! 任何帮助表示赞赏!

You could generate the HTML with id tags for each column and row. 您可以为每个列和行生成带有id标签的HTML。 Then just select by the name instead of using the complicated selectors. 然后只需按名称进行选择,而不使用复杂的选择器。

I would say that $('#table_div td:eq('+colCount*n+')') is a complicated selector, while $('#row1') is much easier. 我会说$('#table_div td:eq('+colCount*n+')')是一个复杂的选择器,而$('#row1')则容易得多。

Also - I believe what you are doing for a browser check will not work. 另外-我相信您正在做的浏览器检查将无法正常工作。 The code you have iterates through all properties for the detected browser and sets the brow value to the last one. 您拥有的代码会遍历检测到的浏览器的所有属性,并将brow值设置为最后一个。 Instead of the loop you have, use something like $.browser.webkit to check for webkit and $.browser.msie to check for IE. 可以使用$.browser.webkit类的东西来检查webkit,使用$.browser.msie来检查IE,而不是循环。 As your code is now, I think you will always be executing the else case. 就像现在的代码一样,我认为您将始终执行else情况。 See here for more on the jQuery browser object: http://api.jquery.com/jQuery.browser/ 有关jQuery浏览器对象的更多信息,请参见此处: http : //api.jquery.com/jQuery.browser/

You need to cache your selectors: 您需要缓存选择器:

var tableDiv = $('#table_div'),
    divHeader = $('#divHeader'),
    firstcol = $('#firstcol'),
    tableFirstCol = $('.tableFirstCol'),
    tds = $('td', tableDiv);

You can use for your column selectors .eq(n). 您可以使用列选择器.eq(n)。

Don't use $(this).css("height", n) when this.style.height = n ; this.style.height = n$(this).css("height", n)请勿使用$(this).css("height", n) is easier and faster. 更容易,更快。

This: 这个:

$('.tableHeader').each(function(i){
    if(m<colCount){
        /* ... */
    }
    m++;
});

should be: 应该:

$('.tableHeader').each(function(i){
    if(i<colCount){
        /* ... */
    }
});

and I'm not entirely sure you would even need the i<colCount check (but I'd have to see the HTML). 而且我不确定,您甚至需要i<colCount检查(但我必须查看HTML)。

The browser sniffing is probably not necessary. 浏览器嗅探可能没有必要。 Try applying a reset stylesheet and a valid doctype or use HTML5Boilerplate . 尝试应用重置样式表和有效的doctype或使用HTML5Boilerplate

If you HAVE to browser sniff: you could just use: 如果您必须浏览器嗅探:您可以使用:

if($.browser.mozilla){
    /* ... */
}
else if ($.browser.msie){
    /* ... */
}
else{
    /* ... */
}

You could also condense that code down quite a lot. 您也可以将代码压缩很多。 Look up the DRY principle . 查找DRY原理

Also, as for setting the height of the tableFirstCol , couldn't you just set the height of the row instead? 另外,关于设置tableFirstCol的高度,您不能只设置行的高度吗?

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

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