3

加载页面时,我想获得面板的顶部。但它显示错误。在 Firefox 和 IE 中,它的说法是“ offset() is undefined ”,而在 Chrome 中,错误是“ Uncaught TypeError: Cannot read property 'top' of undefined ”。请帮忙。

$(window).resize(function () {
    tabPanelAdjust();
});

function tabPanelAdjust() {
    var theTabPanel = $(".TabPanel");
    var tabPanelTop = parseInt(theTabPanel.offset().top);
    var winHeight = $(document).height();
    tabPanelHeight = winHeight - tabPanelTop;
    theTabPanel.css({ "min-height": tabPanelHeight - 20 });
}

<div class="TabPanel">
</div>
4

2 回答 2

9

代码没有问题。在文档就绪方法中包含您的代码。可能是在加载 div 之前触发了您的代码。

$(function(){
$(window).resize(function () {
  tabPanelAdjust();
});

function tabPanelAdjust() {
 var theTabPanel = $(".TabPanel");
 var tabPanelTop = parseInt(theTabPanel.offset().top);
 var winHeight = $(document).height();
 tabPanelHeight = winHeight - tabPanelTop;
 theTabPanel.css({ "min-height": tabPanelHeight - 20 });
}
});

这是一个工作小提琴:

http://jsfiddle.net/SX5U4/

于 2012-11-09T08:32:42.020 回答
2

试试这个:http: //jsbin.com/ecumag/1/edit

function tabPanelAdjust() {
   var theTabPanel = $(".TabPanel");
   alert(theTabPanel.offset().top);
   var tabPanelTop = theTabPanel.offset().top;
   var winHeight = $(document).height();
   tabPanelHeight = winHeight - tabPanelTop;
   theTabPanel.css({ "min-height": tabPanelHeight - 20,"background":"yellow" });
}

$(function(){

    tabPanelAdjust();

});

请确保您正在加载 jquery

于 2012-11-09T08:40:26.140 回答