奇怪的 div 高度错误:webkit 或 jQuery bug?
我有一个简单的网页应用程序,它有两个并排的浮动内容 div。
一小段 jquery 通过查看父 div 的高度并进行简单计算来设置这些 div 的高度
。这种方法在 Firefox 和 Firefox 中效果很好IE..它第一次在 Safari 和 Chrome 中也能工作...
但是当页面在 Safari 或 Chrome 中重新加载时..左右 div 会无缘无故地神奇地增加大约 200px 的高度。
进行一次硬刷新..div 高度恢复正常,
请指教!这很奇怪——
<div class="holder1" >
<!-- LEFT HAND BOX -->
<div id="left-box" class="half left">
<div class="info-box">
<h2>Left Hand Content</h2>
------------- Left content here----------------------
</div>
</div>
<!-- RIGHT HAND BOX -->
<div id="right-box" class="half right" >
<div class="info-box">
<h2>Right Hand Content</h2>
-------------Right content here----------------------
</div>
</div>
<div class="clearfix"></div>
</div>
<!-- END RIGHT and LEFT BOXES -->
<div> more content here </div>
<script language="javascript">
var new_height = $('#left-box').closest('.holder1').height() -25;
$('#right-box').find('.info-box').height(new_height);
$('#left-box').find('.info-box').height(new_height);
</script>
I have a simple web page application that has two floated content divs side by side..
A little piece of jquery sets the height of these divs by looking at the height of the parent div and doing a simple calculation
This approach works great in Firefox and IE.. and it also works the first time in Safari and Chrome...
But when the page is reloaded in Safari or Chrome.. the right and lefts div's magically increase their height by about 200px for no reason.
Do a hard refresh.. and the div heights go back to normal
Please advise! this is very strange -
<div class="holder1" >
<!-- LEFT HAND BOX -->
<div id="left-box" class="half left">
<div class="info-box">
<h2>Left Hand Content</h2>
------------- Left content here----------------------
</div>
</div>
<!-- RIGHT HAND BOX -->
<div id="right-box" class="half right" >
<div class="info-box">
<h2>Right Hand Content</h2>
-------------Right content here----------------------
</div>
</div>
<div class="clearfix"></div>
</div>
<!-- END RIGHT and LEFT BOXES -->
<div> more content here </div>
<script language="javascript">
var new_height = $('#left-box').closest('.holder1').height() -25;
$('#right-box').find('.info-box').height(new_height);
$('#left-box').find('.info-box').height(new_height);
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你确定 DOM 是在 javascript 运行时构建的吗?
尝试
Are you sure the DOM is built when javascript is running ?
Try
试试这个,它对我有用:
Try this, it works for me:
感谢所有的想法..当我将 jQuery include 从 1.4 更新到 1.5 时,它似乎对我很有帮助..
这是一个适用于我在 FF、IE、Safari 和 Chrome 中的小提琴..
http://jsfiddle.net/bytKE/11/
thanks for all the ideas.. it seemed to help most when I update the jQuery include from 1.4 to 1.5..
here's a fiddle that works for me in FF, IE, Safari and Chrome..
http://jsfiddle.net/bytKE/11/
我想我找到了解决方案。
我遇到了类似的问题,尝试根据父 DIV 高度(具有自动高度)更改 DIV(包含浮动 DIV)的高度。它在 IE 上运行得很好,但在 Chrome 上却不行。在 Chrome 中,父 DIV 甚至没有正确的高度,就像 Chrome 不将浮动 DIV 算作父 DIV 的一部分一样。
为了解决这个问题,我必须更改父 DIV 的样式,添加“float:left”以使其也“浮动”。
在您的 CSS 代码中,它将是:
看,现在我们看到红色虚线父 DIV:http://jsfiddle。网/bytKE/46/
I think I found the solution.
I had a similar problem trying to change the height of a DIV (containing floated DIVs) based on the parent DIV height (that had height auto). It was working great with IE, but not with Chrome. In Chrome, the parent DIV didn't even had the proper height, like if Chrome didn't count the floated DIV as part of the parent DIV.
To fix the problem, I had to change the style of the parent DIV in adding "float:left" to make it "float" as well.
In your CSS code, it would be:
See, now we see the red dotted parent DIV: http://jsfiddle.net/bytKE/46/
Webkit 在重新加载或刷新后无法检测元素高度的 jQuery 问题已经存在了一段时间。这些可能的解决方案值得尝试:
方法 1
尝试使用
jQuery(window).load
而不是jQuery(document).ready
,例如:来源
方法 2(超过 90%+ 的浏览器支持,但 IE8 不支持)
如果上述方法导致 Webkit 和其他浏览器之间出现一些问题,请考虑使用此方法:
来源
方法 3 (不是一个优雅的方法,但它在所有浏览器)
如果即使这样也不起作用,您可以尝试一种不太干净的方法,首先检测浏览器,然后应用依赖于脚本的浏览器(无论它是否是 webkit):
另一种方法可能是确保包含任何 CSS 文件 < em>在包含 jQuery 文件之前。
This jQuery issue of Webkit failing to detect the height of an element after a reload or refresh has been around for some time. These possible solutions are worth trying though:
Method 1
Try using
jQuery(window).load
instead ofjQuery(document).ready
, e.g.:Source
Method 2 (supported by over 90%+ of browsers, though not IE8)
If the above approach causes some issues between Webkit and other browsers, consider using this:
Source
Method 3 (not an elegant one, but it has the highest chance of working properly on all browsers)
And if even that is not working it, you coùld try a less clean way by detecting the browser first, and then applying the script dependent whether it's webkit or not:
Another approach may be to make sure to include any CSS files before the inclusion of the jQuery file.