为什么jQuery的div高度返回null? [英] Why does jQuery return null for the height of the div?

查看:207
本文介绍了为什么jQuery的div高度返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$(document).ready(function(){

  function getDocumentHeight()
  {
     // What's the page height?
     var height = $('#mainbody').height();
     alert(height);
   ...
 getDocumentHeight();  }

jQuery不断提醒null我的#mainbody div的高度.我不明白为什么,它至少应该是500px左右.

jQuery keeps alerting null for the height of my #mainbody div. I don't get why, it should at least be 500px or so.

推荐答案

如果动态加载内容,则元素可能尚未达到高度.尝试将这段代码移到页面底部的正上方,看看是否可以解决该问题.

If the content is being loaded dynmically, the elements may not have a height yet. Try moving this code to the bottom of the page, directly above and see if that resolves.

更新: 尝试将代码放入

UPDATE: Try placing your code in

$(window).load(function() {
  var height = $('#mainbody').height();
  alert(height);
});

在您的

$(document).ready();

这篇关于为什么jQuery的div高度返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆