在加载页面时,我想获得面板的顶部。但它显示错误。 在Firefox和IE中,它说“ offset()未定义”,而在Chrome中,错误是“未捕获的TypeError:无法读取未定义的属性'top'”。请帮忙。
$(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>
答案 0 :(得分:9)
代码没有问题。在document ready方法中包含您的代码。可能是你的代码在加载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 });
}
});
这是一个工作小提琴:
答案 1 :(得分: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