jQuery: Header height returns 0 on subsequent pages

元气小坏坏 提交于 2019-12-24 18:23:04

问题


I don't really understand the problem I am having now. I have this code:

$(document).on("pageshow", "[data-role=page]", function(){
   var $height = $("[data-role=header]").height();
   alert($height);
});

This code is working fine on the first page (index) and actually returns an alert with 50. However when I navigate to different pages, the alert returns a height of 0 unless I do a full refresh.

I tried window onload function too, it returns me 0. What I want to do is to get the height of the header for every page and use it to align ui-content properly.

Can anyone explain to me what's going on and what is the right way to do this? There's not much in the header except for a font and a button.


回答1:


You should explicitly specify which header height you really want. Check the following code, hope this works for you.

var $height = $("[data-role=header]:visible").height();



回答2:


I'm not entirely sure what you are trying too do. Are you using a div with buttons inside or something els?

But I would do something like this: http://jsfiddle.net/zJxgh/

$(function() {
    var header = $('#header').height();
    alert(header);
});


来源:https://stackoverflow.com/questions/15627973/jquery-header-height-returns-0-on-subsequent-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!