Javascript 如何通过容器中的元素定义容器的高度

Javascript 如何通过容器中的元素定义容器的高度,javascript,jquery,html,Javascript,Jquery,Html,我试图使用jquery或javascript根据容器中的图像定义容器的高度。这可能吗?我的代码如下: <div class="img-info"> <img src="images/photos/thumb_sample_l.jpg" alt="sample" title="Sample thumbnail" width="100%" /> </div> 这个jQuery脚本应该可以做到这一点 var imgHeight = $('.img-info img').heig

我试图使用jquery或javascript根据容器中的图像定义容器的高度。这可能吗?我的代码如下:

<div class="img-info"> <img src="images/photos/thumb_sample_l.jpg" alt="sample" title="Sample thumbnail" width="100%" /> </div>
这个jQuery脚本应该可以做到这一点

var imgHeight = $('.img-info img').height();
$('.img-info').css({'height':imgHeight});

根据屏幕高度管理内容高度的javascript代码可能对您有所帮助

<script type="text/javascript">
  $(document).ready(function () {
  var scrheight=screen.availHeight;
  //alert(scrheight-270);
  $('.TabbedPanelsContent').css('max-height',scrheight-270);
  $('.TabbedPanelsContent').css('overflow','auto');
  });
  </script>

$(文档).ready(函数(){
var scrheight=screen.availHeight;
//警报(scrheight-270);
$('.tabbedspanelscontent').css('max-height',scrheight-270);
$('.TabbedPanelsContent').css('overflow','auto');
});

使用您的类名而不是TabbedPanels此处的内容将使您想要。

通常容器会隐式地显示其内容的高度。css高度(实际上是宽度)的默认值属性被浏览器自动计算为足够大,可以容纳其中的任何内容。也许OP正试图通过图像设置高度,然后让其余内容通过滚动条溢出?试试这个:
$('.img info').height($('.img info>img').height())
Raminson,这非常有效!谢谢,问题解决了。谢谢abc123,这也将在我正在进行的项目中派上用场。