IE7 中 DHTML 更改后如何强制重排?
我有一个页面,用户可以在其中动态添加文件上传框。添加框会更改它们所在的 div 的高度,但其下方的 div 的某些元素保持在同一位置,因此它们开始与新的 DOM 元素重叠。
这在 IE8、Firefox、Chrome 中可以正常工作。如何强制 IE7 使用新的 DHTML 重排页面?
我找到的最好的解决方案是这样的:
window.resizeBy(1, 0);
setTimeout(UndoResize, 0);
但它不适用于最大化的窗口(它会恢复窗口)。
I have a page where the user can dynamically add file upload boxes. Adding the boxes changes the height of the div they are in, but certain elements of the div below it stay in the same place, so they start to overlap with the new DOM elements.
This works correctly in IE8, Firefox, Chrome. How can I force IE7 to reflow the page with the new DHTML?
The best solution I worked out was this:
window.resizeBy(1, 0);
setTimeout(UndoResize, 0);
But it doesn't work with a maximized window (it restores the window).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
在修改后的 div 上(或者可能是其父级,甚至是更远的祖先,具体取决于各种因素,例如相对定位的包含)。
由于
className
已被分配一个值,IE 将回流并重新绘制页面的该部分,以防影响该元素的 CSS 发生更改。幸运的是,它没有经过优化来检查className
值是否确实较之前的值发生了变化,因此上面的代码将触发回流,而不会破坏其他任何内容。我确实发现有一次这个修复了 IE6 但破坏了 IE7,但是尝试一下,看看它是否适合你。
Try:
on the modified div (or possibly its parent, or even a more remote ancestor, depending on various factors such as relatively-positioned containment).
As the
className
has been assigned a value IE will reflow and repaint that portion of the page in case the CSS affecting that element has changed. Luckily, it isn't optimised to check if theclassName
value actually changed from its previous value, so the above will trigger the reflow without breaking anything else.I did find one occasion when this fixed IE6 but broke IE7, but try it and see if it works for you.
我刚刚在这个绝对荒谬的错误(仅在 IE7 中出现)上浪费了令人难以置信的大量时间,网页太复杂,无法将代码放在这里,其中
element.className = element.className
不是'工作。IE7 的最终解决方案(嗯,至少在我遇到错误的地方进行了测试)似乎是执行下面的所有行作为任何 DOM 更改的钩子:
我们已经有了前两行(用
try-catch
包围)在我们的框架中存在了很长一段时间,但在某些特定场景中它被证明是不够的,但添加接下来的两行修复了这个问题。在最大化和非最大化窗口中进行了测试。
try/catch
之所以存在,是因为在某些特定情况下(例如在iframe
内),它可能会生成一个 JS 错误,从而破坏应用程序(这是我的团队成员提供的信息) ,我自己没遇到过)。相反,对于 IE8,
element.className = element.className
似乎正在完成其工作(是的,我们都喜欢每个版本的条件代码......)我喜欢 Win XP 作为操作系统,但在 IE 束缚的人们使用它之前,我们必须找到针对这些疯狂问题的肮脏修复......该死的悲伤。
编辑 2013.03.05
上面的代码片段似乎适用于大多数场景,但在某个地方还不够。现在我们的代码中有这样的东西:
I've just wasted incredible amounts of time on this absolutely ridiculous bug (manifesting in IE7 only), in the webpage way too complex to put the code here, where
element.className = element.className
wasn't working.The ultimate solution for IE7 (well, tested at least in the place I've encountered the bug) seems to be executing ALL of the lines below as a hook to any DOM changes:
We've already had the first two lines (surrounded with
try-catch
) in our framework for a long time, but it turned out insufficient in some particular scenario, but adding the next two fixed this.Tested both in maximized and non-maximized window.
try/catch
is in place because in some certain circumstances (e.g. inside aniframe
) it may generate a JS error that will break the app (this is info from my team fellow, I haven't encountered it myself).On the contrary, for IE8,
element.className = element.className
seems to be doing its job (yeah, we all love conditional code for each version...)I love Win XP as an OS, but until IE-bound people use it, we have to find dirty fixes to such crazy problems... Damn sad.
Edit 2013.03.05
The snippet above seemed to be working in most of the scenarios, but also was not enough in one place. Now we have stuff like this in our code: