0

我现在有一个代码来修复我的表格的标题,它工作正常。但是这个函数有一个警告:

Error: TypeError: $(...).offset(...) is undefined

我的代码(我使用引导程序):

function goheadfixed(classtable) {
$(classtable).wrap('<div class="fix-inner">'); 
$('.fix-inner').wrap('<div class="fix-outer" style="position: relative;"></div>'); //this is relative cause the header will be absolute
$('.fix-outer').append('<div class="fix-head"></div>');
$('.fix-head').prepend($('.fix-inner').html()); // agrego la tabla
$('.fix-head table').find('caption').remove();
//$('.fix-head table').removeAttr('style');
$('.fix-head table').css('width','100%');

$('.fix-head').css('width', $('.fix-inner table').outerWidth(true)+'px');
$('.fix-head').css('height', $('.fix-inner table thead').outerHeight(true)+'px');

var ithead = parseInt($('.fix-inner table thead').offset().top);
var divfix = parseInt($('.fix-inner').offset().top);
var itop = ithead-divfix;

$('.fix-head').css({'position':'absolute', 'overflow':'hidden', 'top': itop+'px', 'left':0, 'z-index':100 });

$(window).scroll(function () {
    var vscroll = $(window).scrollTop();

    if(vscroll >= ithead)
        $('.fix-head').css('top',(vscroll-divfix)+'px');
    else
        $('.fix-head').css('top', itop+'px');
});

/*  If the windows resize   */
$(window).resize(goresize);
}

function goresize() {
$('.fix-head').css('width', $('.fix-inner table').outerWidth(true)+'px');
$('.fix-head').css('height', $('.fix-inner table thead').outerHeight(true)+'px');
}

我调用我的函数:

goheadfixed('table.fixed');

然后,当我将其他代码 javascript 放在下面时,我的代码不起作用,但是放在上面时,它可以正常工作!:

如何删除此警告?

编辑(添加作为答案发布的详细信息):哦。对不起,我忘了说“警告”只有在我不使用该功能时才会出现。

如果我调用函数 goheadfixed('table.fixed'); 好的,但是如果我不调用这个函数,就会显示警告。

4

1 回答 1

0

在第 14 行,$('.fix-inner table thead')指的是不存在或隐藏的元素。听起来好像您至少找到了一个带有display:none集合的元素,因此它返回了一个未定义的数字。

$("thead:visible")要解决此问题,您可以为每个元素添加可见选择器。

于 2013-01-23T23:02:30.937 回答