jQuery .height() 与 chrome 的问题
我一直在使用 jQuery Height 函数。但是获取 div
元素的正确高度时出现问题。此 div
元素的高度设置为 auto
,这意味着 div
的高度仅取决于其内部的元素。当我尝试在页面中使用 .height()
时,我得到了以下结果:
Chrome: 1276 px
Firefox: 1424 px
但是当您看到它们时,它们的高度相等。唯一的问题是 height()
函数返回不同的结果。
代码如下:
<div class="single-mid" style="position:relative;width:700px;margin:-1px 0 0 1px;padding-right:56px;">
<div>Sample Inside Element</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
var less_height = $('.single-mid').height() -10;
$('.single-mid').height(less_height);
});
</script>
但我尝试将div的高度设置为1424px。两个浏览器返回相同的结果。
有什么想法吗?提前致谢。
I've been using jQuery Height function. But there was a problem in getting the correct height of my div
element. This div
element's height is set to auto
, which means the div
's height depends only on the elements inside of it. When I tried using the .height()
in my page, I got the following result:
Chrome: 1276 px
Firefox: 1424 px
But when you see them both they have equal height. The only thing is that the height()
function returns different result.
Here's the code:
<div class="single-mid" style="position:relative;width:700px;margin:-1px 0 0 1px;padding-right:56px;">
<div>Sample Inside Element</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
var less_height = $('.single-mid').height() -10;
$('.single-mid').height(less_height);
});
</script>
But I tried to set the div's height into 1424px. Both browser return the same result.
Any ideas? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我遇到了同样的问题,在使用 Google Fonts 的字体时,它隐式设置了 1.5em 的行高,而 jQuery 可能没有看到这一点。一旦我明确地将 line-height:1.5em 写入我的 CSS 重置(或网站字体设置),就可以了。
I had the same issue, while using font from Google Fonts and it had implicitly set line-height of 1.5em, which the jQuery probably did not see. Once I explicitly wrote line-height:1.5em to my CSS reset (or website font setting), it was OK.
我对引导下拉菜单有同样的问题,元素必须高度标准化。
对于某些菜单(不是全部),jquery 没有返回正确的高度,导致标准化不良。
这是因为下拉菜单在标准化过程中被隐藏了。当元素隐藏时,CHROME 和 FIREFOX 似乎无法正确计算高度。在这种情况下,IE 似乎工作得更好。
为了解决这个问题,我向所有菜单添加(然后删除)引导类“open”,以便使它们在规范化期间可见:
I had the same issue with bootstrap dropdown menu that elements had to be height normalized.
For some menus (not all), jquery did not return the right height, resulting in bad normalization.
It was because dropdown menus were hidden during the normalization. It seems that CHROME and FIREFOX does not compute the height correctly when the element is hidden. IE seems to work better in this case.
To solve the issue, I have add (then remove) the bootstrap class "open" to all menus, in order to make them visible during the normalization:
尝试两种方法来固定 Chrome 和 IE 的
高度
Try two method to fixed height in chrome and IE
And
这发生在我身上,以及我计算高度的
div
中的某些图像没有设置height
属性的原因。$(window).load(
确实对我有用,但对我想做的事情造成了太多的延迟。This happened to me and the reason what that some images in the
div
that I was calculate the height of did not have theheight
attribute set.$(window).load(
did work for me, but caused too much of a delay for what I wanted to do.看来是两个浏览器默认字体不同造成的。如果您将以下 CSS 添加到示例中,则 Firefox 和 Chrome/Iron 的结果将相同:
您可以使用 CSS 重置 并自己定义样式。这将使您能够更好地控制如何使其在大多数浏览器中看起来相同。
It seems to be caused by the different default font in the two browsers. If you add the following CSS to your example, the result will be the same for both Firefox and Chrome/Iron:
You could probably use a CSS reset and define the styles yourself. This would give you more control on how to get it to look the same in most browsers.
有同样的问题。但是如果我在进行高度检查之前做了短暂的 setTimeout 延迟,chrome 就会开始报告与 firefox 相同的高度。看来 Chrome 在完全确定内容将如何改变容器的实际大小之前触发了文档就绪状态...!?无论如何,我的解决方法是将 my: 更改
为 a: ,
直到“所有子元素已完全加载”(来自 http://api.jquery.com/load-event/)。
Had the same issue. But if I did a brief setTimeout delay before doing the hight check, chrome started reporting the same height as firefox. It seems chrome triggers the document ready state before fully determining how the content will alter the actual size of the container...!? In any case, my fix was to change my:
to a:
which doesn't trigger until "all sub-elements have been completely loaded" (from http://api.jquery.com/load-event/).