Javascript jQuery - 设置 div 的最小高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2750892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - Set min-height of div
提问by Kenny Bones
This is probably really easy for most of you. But I'm in need of a small snippet that looks up the current height of a div (the div has a dynamic height based on the content inside it) and then set that value in the css class's min-height value.
这对你们大多数人来说可能真的很容易。但是我需要一个小片段来查找 div 的当前高度(div 具有基于其中的内容的动态高度),然后在 css 类的 min-height 值中设置该值。
Basically what this means is that I want this container to have it's min-height to be the exact same value as it's current height. This is probably a quick one :)
基本上这意味着我希望这个容器的最小高度与它的当前高度完全相同。这可能是一个快速的:)
采纳答案by Luca Filosofi
just a proof of concept!
只是一个概念证明!
// attend for dom ready
$(function() {
// hide .foo before we get it's height
$('.foo').hide();
// get the height, also the one mentioned below is a good one!
var foo_height = $('.foo').height();
// see if the actual height respect our needs! esample 180px
if ( foo_height > 180 ) {
// set the height and show it!
//$('.foo').css('height', foo_height + 'px').show(); OR try
$('.foo').height( foo_height ).show();
} else {
//do something else here
}
});
this should work as expected!
这应该按预期工作!
回答by Sampson
If I understand you correctly, you could do the following:
如果我理解正确,您可以执行以下操作:
$(".foo").css("min-height", function(){
return $(this).height();
});
回答by Denis Maslov
var elt = document.getElementById("<%= this.your_element_id.ClientID %>");
elt.style.minHeight = elt.clientHeight + 'px';

