当父对象设置为none时,如何获取对象的最小高度? [英] How to get the min-height of the object when its parent is set to display none?
问题描述
当父设置为显示时,为什么我无法获取对象的
,但如果最小高度不,我仍然可以使用对象的高度吗? min-height
:没有
Why I can't get the min-height
of an object when its parent is set to display: none
, but I still can it the object's height if the min-height is not in use?
For例如,
css,
li {
display:none;
}
.object {
display:block;
width:100px;
border:1px solid #000;
}
html,
<li><a href="#" class="object" style="height:200px;"><img class="element" /></a></li>
<li><a href="#" class="object" style="min-height:300px;"><img class="element" /></a></li>
jquery,
$(document).ready(function(){
$('.object img').each(function(){
alert($(this).parent().height());
});
});
我怎样才能获得对象的 min-height
,即使它是父对象设置为显示无
?
How can I still get the min-height
of the object even though its parent is set to display none
?
推荐答案
当元素不是显示,它没有高度或宽度。
When an element isn't displayed, it has no height or width.
您可以提取CSS属性,但是:
You can extract the CSS attribute, though:
alert($(this).parent().css('min-height'));
请注意,这现在返回一个末尾带有px的字符串,而不是像 height()
这样的数字。您可能需要将其解析为整数:
Note that this now returns a string with "px" at the end, instead of a number like height()
does. You may need to parse it as an integer:
alert( parseInt($(this).parent().css('min-height'),10) );
显然,如果没有 min-height
设置在CSS中,这不起作用。根据您想要的数字,您可能需要添加一些编程逻辑,如果没有返回最小高度,则提取 .css('height')
。
Obviously if there's no min-height
set in CSS, this won't work. Depending on what you want the number for, you may need to add some programmatic logic that extracts .css('height')
if there's no min-height returned.
$(document).ready(function(){
$('.object img').each(function(){
var h = parseInt($(this).parent().css('min-height'),10)
|| parseInt($(this).parent().css('height'),10);
alert(h);
});
});
最后,请记住您从中获取的值 .css
不一定是元素最终显示时的高度 - 它们只是你想要的高度。
Finally, remember that the values you're getting from .css
aren't necessarily what the height will be when the element is finally displayed -- they're only what you want the height to be.
这篇关于当父对象设置为none时,如何获取对象的最小高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!