简体   繁体   English

更新DOM时,相对定位的元素不会移动(IE6和IE7)

[英]Elements positioned relatively don't move when the DOM is updated (IE6 and IE7)

I have a form with a few fieldsets. 我有一个包含几个字段集的表单。 One fieldset has a table of time preferences set by the user. 一个字段集具有由用户设置的时间偏好表。 The user can add and delete time preferences. 用户可以添加和删除时间首选项。 When they add a row, a table row is dynamically inserted into the DOM using a jQuery append() . 当他们添加一行时,使用jQuery append()表行动态插入到DOM中。

The issue is that in IE6 and IE7 any relatively positioned elements on the page do not "bump" down when the new table rows are added. 问题是在IE6和IE7中,当添加新表行时,页面上任何相对定位的元素都不会“碰撞”。 Furthermore, they don't move when table rows are deleted either. 此外,当删除表行时,它们也不会移动。 They are kind of just stuck in their spots. 他们有点卡在他们的位置。

This would be relatively minor, but each fieldset is relatively positioned in order to avoid the IE background overflow issue with fieldsets. 这将是相对较小的,但每个字段集相对定位,以避免与字段集的IE后台溢出问题。 Therefore, the form likes quite bad after one adds two or more rows to the table. 因此,在向表中添加两行或更多行之后,表单非常糟糕。

Here is the CSS being applied to the fieldsets: 以下是应用于fieldsets的CSS:

form.pancake fieldset {
    position: relative;
    margin-top: 1.5em;
    padding-top: 1.5em;
}
form.pancake fieldset legend {
    position: absolute;
    top: -0.5em;
    left: 0.5em;
}

When the position: relative is removed from the stylesheet, the dynamically added rows work perfectly and the content moves down as appropriate. 当从样式表中删除position: relative ,动态添加的行可以完美地工作,并且内容会根据需要向下移动。

Any help is much appreciated. 任何帮助深表感谢。

Yeah IE is a real pain when it comes to this. 是的,IE就是一个真正的痛苦。 I found that I actually had to force it to redraw the DOM element inorder to get it to move. 我发现我实际上不得不强制它重绘DOM元素以便让它移动。 How I did this was to very quickly hide and show the parent object in your case it sounds like the parent to your row. 我这样做是为了非常快速地隐藏并在你的情况下显示父对象它听起来像你的行的父。 This is not the most elegant solution, but it does the job. 这不是最优雅的解决方案,但它可以完成这项工作。

In my case I used jQuery to get the job done. 在我的例子中,我使用jQuery来完成工作。

var forceRedraw = function(obj) {
    /// <summary>Forces a redraw of passed objects.</summary>
    /// <param name="obj" type="jQuery" optional="false">The object for force the redraw on.</param>

    obj = $(obj);

    // force IE to redraw the obj
    if (jQuery.browser.msie) {
        obj.css("display", "none");
        obj.css("display", "block");
    }
};

I recently encountered the same problem. 我最近遇到了同样的问题。 I ended up finding that this only happened when my relatively positioned element was contained within another relatively positioned element. 我最终发现这只发生在我相对定位的元素包含在另一个相对定位的元素中时。

Although it wasn't possible for me to change the positioning style of the element itself, I was able to remove position:relative from the containing element, and it solved the problem. 虽然我不可能更改元素本身的定位样式,但我能够从包含元素中移除position:relative,并解决了问题。 This might be a good alternative solution, especially on a page with lots of actions that could cause elements to move. 这可能是一个很好的替代解决方案,尤其是在包含大量可能导致元素移动的操作的页面上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM