为什么jquery返回0为offsetHeight当firebug说它是34? [英] Why would jquery return 0 for an offsetHeight when firebug says it's 34?

查看:116
本文介绍了为什么jquery返回0为offsetHeight当firebug说它是34?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个div的内容是在运行时生成的,它最初没有与它相关联的高度。当它根据firebug生成,从我可以用js提醒时,div仍然具有0的高度。但是,使用firebug查看只读属性,我可以看到它的偏移高度为34.这是值我需要。希望它是明显的,但如果不是这个数字是可变的,并不总是38。



所以,我以为我可以通过这样做jquery ...

  $(#parentDiv)。attr('offsetHeight'); 

或直接js ...

 的document.getElementById( parentDiv)的offsetHeight。 

但是返回的所有值都是0.与偏移高度是在这种情况下只读属性?如何获得这个高度?我的意思是firebug正在以某种方式弄清楚它,似乎我应该能够。



编辑:
以下是现在的div的样例...

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML Strict // EN>< META http-equiv =Content-Type content =text / html; charset = utf-8> 

< HTML style =OVERFLOW:hidden; HEIGHT:100%xmlns =http://www.w3.org/1999/xhtml>< BODY>< FORM id =aspnetFormname =aspnetFormaction =blah.aspxmethod =post>< DIV id =container>

< DIV id =ctl00_BodyContentPlaceHolder_Navigationstyle =Z-INDEX:1; LEFT:1597px; POSITION:absolute; TOP:67px>
< DIV class =TransparentBgid =TransparentDivstyle =MARGIN-TOP:10px; MARGIN-RIGHT:10px; HEIGHT:94px; TEXT-ALIGN:center>
< / DIV>
< DIV class =Foregroundid =ForegroundIdstyle =MARGIN-TOP:10px; MARGIN-RIGHT:10px; TEXT-ALIGN:center>
< DIV id =ctl00_BodyContentPlaceHolder_Navigation1style =WIDTH:52px; COLOR:black; HEIGHT:52px; BACKGROUND-COLOR:transparent; -moz-user-focus:normal>
< IMG style =FILTER:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod = scale src ='.. / images / image.gif'); CURSOR:指针height =52hspace =0 src =...width =52/>
< / DIV>
< DIV id =ctl00_BodyContentPlaceHolder_UserControlname =ctl00_BodyContentPlaceHolder_UserControl>
< IMG style =DISPLAY:block; VERTICAL-ALIGN:bottom; CURSOR:pointerheight =17src =...width =16/>
< IMG style =VERTICAL-ALIGN:top; CURSOR:pointerheight =17src =...width =16/>
< / DIV>
< / DIV>
< / DIV>

< / DIV>< / FORM>< / BODY>< / HTML>

此代码由单独库中的控件生成。所以这里是我的.aspx页面中的实际代码。

 < blah:blahControl ID =controlrunat =服务器/>好的,它稍作修改,但是比以前更多的HTML。之前被称为parentDiv的div在那里被称为ctl00_BodyContentPlaceHolder_UserControl。该代码包括有问题的div,它是兄弟,父母,祖父母和孩子。这几乎是firebug的直接副本。



更新:
我应该提到这是在IE 7中完成的。在Firefox中工作一次,但现在返回0。这是否提供了可能的解决方法或任何新的见解?



...你们都必须认为我是坚果。



更新:
某些样式...

  .TransparentBg 
{
background-color:white;
过滤器:alpha(opacity = 50);
-moz-opacity:0.5;
-khtml-opacity:0.5;
不透明度:0.5;
}

.Foreground
{
position:absolute;
顶部:0px;
}


解决方案

你确定它不是 Heisenbug ?如果您以编程方式设置高度,然后稍后尝试阅读,那么DOM将不会更新。


So I have a div whose content is generated at runtime it initially has no height associated with it. When it's generated according to firebug and from what I can alert with js the div still has a height of 0. However, looking at the read-only properties with firebug I can see that it has an offset height of 34. It's this value that I need. Hopefully it's obvious but in case it isn't, this number is variable, it's not always 38.

So, I thought that I could just get that by doing this via jquery...

$("#parentDiv").attr('offsetHeight');

or this with straight js...

document.getElementById("parentDiv").offsetHeight;

But all that is returned is 0. Does it have anything to do with the fact that offset height is a read-only property in this instance? How can I get this height? I mean firebug is figuring it out somehow so it seems like I should be able to.

Edit: Here's a sample of what the div looks like right now...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML Strict//EN"><META http-equiv="Content-Type" content="text/html; charset=utf-8">

<HTML style="OVERFLOW: hidden; HEIGHT: 100%" xmlns="http://www.w3.org/1999/xhtml"><BODY><FORM id="aspnetForm" name="aspnetForm" action="blah.aspx" method="post"><DIV id="container">

<DIV id="ctl00_BodyContentPlaceHolder_Navigation" style="Z-INDEX: 1; LEFT: 1597px; POSITION: absolute; TOP: 67px">
    <DIV class="TransparentBg" id="TransparentDiv" style="MARGIN-TOP: 10px; MARGIN-RIGHT: 10px; HEIGHT: 94px; TEXT-ALIGN: center">
    </DIV>
    <DIV class="Foreground" id="ForegroundId" style="MARGIN-TOP: 10px; MARGIN-RIGHT: 10px; TEXT-ALIGN: center">
        <DIV id="ctl00_BodyContentPlaceHolder_Navigation1" style="WIDTH: 52px; COLOR: black; HEIGHT: 52px; BACKGROUND-COLOR: transparent; -moz-user-focus: normal">
            <IMG style="FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod = scale src='../images/image.gif'); CURSOR: pointer" height="52" hspace="0" src="..." width="52" /> 
        </DIV>
        <DIV id="ctl00_BodyContentPlaceHolder_UserControl" name="ctl00_BodyContentPlaceHolder_UserControl">
            <IMG style="DISPLAY: block; VERTICAL-ALIGN: bottom; CURSOR: pointer" height="17" src="..." width="16" />
            <IMG style="VERTICAL-ALIGN: top; CURSOR: pointer" height="17" src="..." width="16" />
        </DIV>
    </DIV>
</DIV>

</DIV></FORM></BODY></HTML>

This code is being generated by a control in a separate library. So here's the actual code creating it in my .aspx page.

<blah:blahControl ID="control" runat="server" />

Ok, it's edited slightly but thats a whole lot more HTML than I had before. The div I was referring to as "parentDiv" before is called "ctl00_BodyContentPlaceHolder_UserControl" up there. That code includes the div in question, it's sibling, parent, grandparent and children. It's almost a direct copy from firebug.

Update: I should have mentioned this is being done in IE 7. It seemed to work fine one time in Firefox, but it's returning 0 now. Does this provide any new insights of possible work-arounds or anything?

... You all must think I'm nuts.

Update: Some styling...

.TransparentBg 
{
    background-color: white;
    filter:alpha(opacity=50);
    -moz-opacity:0.5;
    -khtml-opacity: 0.5;
    opacity: 0.5;
}

.Foreground
{
    position: absolute;
    top: 0px;
}

解决方案

Are you sure it's not a Heisenbug? If you are setting the height somewhere programmatically and then trying to read it soon later, there is a chance DOM would not have updated.

这篇关于为什么jquery返回0为offsetHeight当firebug说它是34?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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