为什么$(someElement).height()从来没有正确地为我计算高度
Why does $(someElement).height() never calculate the height correctly for me?
我有一个程序
jQuery(function($){
function makeParentHeightOfSmallestChild (parentSelector)
{
var parent = $(parentSelector);
var children = parent.children();
if (children.length < 2) return;
var smallestHeight = Number.MAX_VALUE;
children.each(function(){
var thisHeight = $(this).height();
if (thisHeight < smallestHeight) smallestHeight = thisHeight;
});
parent.height(smallestHeight);
}
$(window).load(function(){
makeParentHeightOfSmallestChild('#contact-page-container > .row');
});
$(window).resize(function(){
makeParentHeightOfSmallestChild('#contact-page-container > .row');
});
});
是不言自明的。我遇到的问题是,smallestHeight
得到的值明显小于元素的实际最小高度,而元素的实际最小高度是通过迭代来计算的。元素就是
<div class="container" id="contact-page-container">
<h1>Get in touch</h1>
<div class="row">
<div class="col-xxs-12 col-xs-12 col-sm-6 col-md-6 col-lg-6">
<h3>Email</h3>
<p>S,madsn sandaskldasjk laskldaskjd skajkasdkjas k</p>
<input type="text" value="Your Name"/>
<input type="text" value="Your Email Address" />
<textarea value="Your Message" rows="18">Your Message</textarea>
</div>
<div class="col-xxs-12 col-xs-12 col-sm-6 col-md-6 col-lg-6">
<h3>Address</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<iframe class="same-height-as-width" src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2686.2302199264063!2d-122.13376389999996!3d47.67994880000002!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x54900d52444ba747%3A0x577e2db631b0711c!2s8620+154th+Ave+NE%2C+Redmond%2C+WA+98052!5e0!3m2!1sen!2sus!4v1441747735537" frameborder="0" allowfullscreen></iframe>
</div>
</div>
,当我在Inspect Element中查看高度是595.556像素和747.778像素,但不知何故,父元素的高度被设置为242像素。为什么呢?
您应该考虑.height()
和.outerHeight()
之间的区别
这是一个很好的链接,祝你好运!
jQuery height() vs outerHeight()http://princepthomas.blogspot.com.br/2011/07/jquery-height-vs-outerheight.html.height ()
获取匹配元素集合中第一个元素的当前计算高度。不包括内边距、边框和边距。
.outerHeight ([includeMargin])
获取匹配元素集合中第一个元素的当前计算高度,包括内边距、边框和可选的边距。
"includeMargin"是一个可选的布尔值,表示是否在计算中包含元素的边距。
// returns height of browser viewport $(window).height(); // returns height of HTML document $(document).height(); // returns height of "IdofYourDIV" excluding padding, border and margin $("#IdofYourDIV").height(); // returns height of "IdofYourDIV" including padding and border but excluding margin $("#IdofYourDIV").outerHeight(); // returns height of "IdofYourDIV" including padding, border and margin $("#IdofYourDIV").outerHeight(true);
在你的代码中加入normalize.css后,height的值变成整数。您可以在jsFiddle的示例中看到,我还隐藏了.row
之外的所有parentSelector
。注意,我看到最后你的代码错过了关闭标签</div>
,也许这是一个复制粘贴错误,但要小心。
相关文章:
- 如何计算每个元素's的高度,并将这些值用作函数中的变量
- 使用jquery计算包含另一个带边距元素的元素的高度
- 如何计算缩放后画布的新高度和宽度
- 获取计算高度-Javascript-而不是jQuery
- Chrome和Firefox之间的高度计算不同
- 无法从 AngularJS 中的元素计算偏移高度
- 展开预览(计算高度:函数)
- Div 不会自动计算高度
- 新偏移量更新字体大小后不计算高度
- 根据点击量或数字计算高度
- 调整大小时重新计算高度
- 在TextArea的自动大小上调整计算高度,理想地工作吗?(jsfiddle - updated)
- 为什么$(someElement).height()从来没有正确地为我计算高度
- 元素的大小需要重新计算高度,因为JSON数据被过滤
- 位置相对内容计算高度
- 沙狐不正确地计算高度
- 调整大小和计算高度- bug
- 绝对定位元素的计算高度
- 如何使用jquery计算高度
- 弹出窗口计算高度