jQuery:获取 jQuery 中隐藏元素的高度 [英] jQuery: Get height of hidden element in jQuery
本文介绍了jQuery:获取 jQuery 中隐藏元素的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要获取隐藏的 div 中元素的高度.现在我显示 div,获取高度,并隐藏父 div.这似乎有点傻.有没有更好的办法?
I need to get height of an element that is within a div that is hidden. Right now I show the div, get the height, and hide the parent div. This seems a bit silly. Is there a better way?
我使用的是 jQuery 1.4.2:
I'm using jQuery 1.4.2:
$select.show();
optionHeight = $firstOption.height(); //we can only get height if its visible
$select.hide();
推荐答案
你可以做这样的事情,虽然有点hacky,如果它已经是绝对的,请忘记position
:
You could do something like this, a bit hacky though, forget position
if it's already absolute:
var previousCss = $("#myDiv").attr("style");
$("#myDiv").css({
position: 'absolute', // Optional if #myDiv is already absolute
visibility: 'hidden',
display: 'block'
});
optionHeight = $("#myDiv").height();
$("#myDiv").attr("style", previousCss ? previousCss : "");
这篇关于jQuery:获取 jQuery 中隐藏元素的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文