jQuery height()在可见div上返回0 - 为什么? [英] jQuery height() returns 0 on a visible div - why?

查看:251
本文介绍了jQuery height()在可见div上返回0 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个容器(thetext1)设置高度为360px。 thetext1包含两个div - 一个在左边,一个在右边 - 这两个内容通过ajax调用传递。



这些div中的一个或另一个的内容超过360px,因此我想相应地增加text1的高度。



我的测试代码

  newhgt = $('#thetext1')。find('div.rhs')。css(background,pink)。 

返回0 - (我的选择器是正确的,因为目标div完全是粉红色的! >

为什么?我知道 - 从在这个网站的以前的帖子的答案,解决方案是添加overflow:隐藏thetext1,但我想知道为什么我的尝试获得rhs和lhs div的高度是失败。

解决方案

确保代码在$(window).load [not $(document).ready]里面

  $(window).load(function(){
newhgt = $('#thetext1')。find('div.rhs')。 css(background,pink)。height();
});


I have a container (thetext1) with a set height of 360px. "thetext1" contains two divs - one on the left and one floated to the right - into both of which content is delivered through an ajax call.

There will be times when the content in one or other of these divs exceeds 360px and so I want to increase the height of thetext1 accordingly.

My test code

newhgt = $('#thetext1').find('div.rhs').css("background", "pink").height();

returns 0 - (my selector is correct as the targeted div is perfectly pink!).

Why is this? I know - from answers to previous posts on this site - that the solution is to add overflow: hidden thetext1, but I would like to understand why my attempt to get the height of the rhs and lhs div is failing.

解决方案

Make sure the code is inside the $(window).load [not $(document).ready ]

$(window).load(function () {
    newhgt = $('#thetext1').find('div.rhs').css("background", "pink").height();
});

这篇关于jQuery height()在可见div上返回0 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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