English 中文(简体)
jj 查询: 当父身高为 0px 时获得子元素的高度
原标题:jQuery: get height of child element when parent height is 0px

我有一个 div 包含文本的 div 文本, 仅在特定情况下显示在另一个 div 中。 HTML 简单, 看起来像 :

<div id= parent >
  <div id= child >
    Some text inside child div
  </div>
</div>

外形看起来像:

#parent{ display:none; height:0px; }

利用jQuery,我想得到孩子的身高,就像我一样,如果div是在父母外。

下面的喷射针返回0, 我希望大约15点左右。

var child_height = $( #child ).height();
alert(child_height);
最佳回答

尝试所有这些 只是工作顺利:

console.log($( #child ).outerHeight());

console.log($( #child ).css( height ));

console.log($( #child ).height());

根据您编辑的 :

var height = $( #parent ).css({visibility:  hidden , display :  block }).find( #child ).height();

$( #parent ).css({visibility:  hidden , display:  none });

console.log(height);

http://jsfidd.net/zjpav/6/" rel=“不跟随” >DEMO

问题回答

你可以暂时给隐藏的父母看 然后隐藏在:

// .parents means it will search through all parents, not just first hidden
var hiddenParents = $("#child").parents(":hidden").show();
var childHeight = $("#child").height();
hiddenParents.hide();

alert(childHeight);

http://jsfiddle.net/zjpav/3/" rel="no follow" >http://jsfiddle.net/zjpav/3/

我注意到你说:“只有在特定情况下才能显示另一个 div ” 。 如果父 div 有 < code> display: nor; (例如,如果你使用多个 jQuery 函数中的任何函数隐藏它, 则它最终会变成这样), 那么孩子的高度将返回为 0 。

var child_height = $( #child ).outerHeight();
alert(child_height);​

"http://jsfiddle.net/mprabhat/kj8mg/1/" rel="nofollow" >Demo

虽然在FF 10上 你的原代码 工作正常。

按注释更新 :

对于隐藏字段,它会返回零, 您可以做类似的事情 :

jQuery( #parent ).css( display , block );
var child_height = $( #child ).outerHeight();
alert(child_height);

child_height = $( #child ).height();
alert(child_height);
jQuery( #parent ).css( display , none );

""http://jsfiddle.net/mprabhat/kj8mg/2/" rel="no follow" >Demo





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签