【问题标题】:jQuery container height() not calculated properly未正确计算 jQuery 容器高度()
【发布时间】:2011-09-30 14:24:11
【问题描述】:

$('.container').height(); 在其子元素具有间距 CSS 属性时给出错误值:

<div class="container">
  <div style="padding: 10px;">
      weiorng3poin4gr9p34ng9p34n5g934ng5o43ng534g
      <div style="margin: 20px; ">
         eiong349ng49ng934ng59p34n5g439g5
      </div>
  </div>

</div>

我怎样才能得到.container的正确高度?

【问题讨论】:

  • 它以什么方式给出“错误值”?太多,太少?一个 jsfiddle.net 示例也会有所帮助。
  • 听起来你可能想要 .outerHeight();
  • 边距是否缺少x
  • 是的,但仅此而已。在我的代码中,边距在 css 文件中..
  • 嘿 outerHeight() 而不是 height() 有效!谢谢!你应该把它作为答案发布

标签: jquery css jquery-selectors height


【解决方案1】:

容器&lt;div&gt; 是否有任何填充/边距/边框?如果是这样,你可以试试这个:

$(".container").outerHeight(true);//will consider margin and border width as well.

【讨论】:

  • 如果我添加“真”,它会给我准确的高度。如果我把它排除在外,那么高度会更接近真实高度,但仍然是错误的。谢谢
  • outHeight() 在这种情况下无法正常工作,因为.container div 上没有marginpadding。这就是计算发生的地方。见api.jquery.com/outerHeight
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
  • 2014-08-18
  • 2014-07-19
相关资源
最近更新 更多