简体   繁体   English

添加/删除CSS将导致IE9增加表的高度

[英]Add/remove CSS will cause IE9 to increase the table's height

I add a mouse event to the HTML TR when user mouse-over/out the TR to change some CSS color. 当用户鼠标悬停在TR上以改变一些CSS颜色时,我向HTML TR添加一个鼠标事件。 But in IE9 seems to have an issue that the table's height will keep increasing each time the CSS changed. 但是在IE9中似乎存在一个问题,即每次CSS更改时表的高度都会不断增加。

Note: the issue only occurs when the horizontal scrollbar appears. 注意:只有出现水平滚动条时才会出现此问题。

Here is the HTML. 这是HTML。

<div style="width:100%;height:100%;">
    <div class="grid">
        <div class="grid-body">
            <table width="100%" cellpadding="0" cellspacing="0" border="0" style="table-layout:fixed;">
                <tbody>
                    <tr>
                        <td style="width:3040px;" class="item">
                            <div>
                                Please mouse-over me and out
                            </div>
                        </td>
                        <td class="item">
                            <div>
                                Please mouse-over me and out
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

Here is the Javascript 这是Javascript

$(document).ready(function (){
 $('.item').mouseover(function () {
     $(this).parent().addClass("item-over");
 }).mouseout(function() {
     $(this).parent().removeClass("item-over");
 });
 }
);

Here is the CSS setting 这是CSS设置

 html, body {height:100%}
 body {
     margin: 0; padding: 0 5px;
 }
 div.grid {
     background: #DAE7F6; border: 1px solid #86A4BE; overflow: hidden; zoom: 1;
 }
 div.grid-body {
     background: red; border: 0; overflow: auto; width: 100%; position: relative;
 }
 tr.item-over {
     color: #6eadff;
 }

You can run the full example here . 您可以在此处运行完整示例。

Here's another possible fix that also seems to work in my case. 这是另一个可能的解决方案,似乎也适用于我的情况。

Setting ANY margin (top, right, bottom, left, or any combination) to 'auto' seems to solve it. 将任何边距(顶部,右侧,底部,左侧或任意组合)设置为“自动”似乎可以解决它。

div.grid-body {
    margin: 0px auto;
}

Or: 要么:

div.grid-body {
    margin-top: auto;
}

Etc. 等等。

Another possible fix suggested in the blog post IE9 Hover Bug Workaround : 另一个可能的修复建议在博客文章IE9 Hover Bug解决方法

div.grid-body {
    min-height: 0%;
}

In case anyone came here looking for a fix for datatables jquery plugin, the proper class to add this fix to is: 如果有人来到这里寻找数据表jquery插件的修复程序,添加此修复程序的正确类是:

.dataTables_scrollBody
{
    margin-top:auto;
}

Took a bit of digging to find the proper div, so I figured I would try to save someone time. 花了一些时间去寻找合适的div,所以我想我会试着节省一些时间。

I might have just solved it. 我可能刚刚解决了它。

Try: 尝试:

width: 100%;
display: inline-block;

on the containing element ("div.grid-body" in this case). 在包含元素(在这种情况下为“div.grid-body”)。

It stops doing it and yet does the mouse hover effect by setting: 它停止这样做,但通过设置鼠标悬停效果:

div.grid-body {
     background: red; border: 0; overflow: hidden; width: 100%; position: relative;
 }

instead of overflow:auto. 而不是溢出:自动。 Mabe you'd prefer to use overflow:scroll or visible. Mabe你更喜欢使用溢出:滚动或可见。 And make it trigger this as an extra property only for the IE9 case. 并使其作为IE9案例的额外属性触发它。

Open Developer tools and remove the table-layout:fixed rule from the table that is the child of grid-body. 打开开发人员工具并从作为grid-body子项的表中删除table-layout:fixed规则。 it should work may be. 应该可以工作。

remove with form the first 'TD' element <td style="width:3040px;" 从表单中删除第一个'TD'元素<td style="width:3040px;" . It will help. 我会帮你的。

Do you need so huge "td" there ? 你需要那么大的“td”吗?

just to see 只是看看

div.grid { background: #DAE7F6; div.grid {background:#DAE7F6; border: 1px solid #86A4BE; 边框:1px solid#86A4BE; overflow: hidden; 溢出:隐藏; zoom: 1; zoom:1; width:100%; 宽度:100%; } }

what about this:
    width:100% !important; 

if you can change the overflow try this 如果你可以改变溢出试试这个

div.grid-body { background: red; div.grid-body {background:red; border: 0; 边界:0; overflow: hidden; 溢出:隐藏; width: 100%; 宽度:100%; position: relative; 位置:相对; } }

else 其他

change your script to this (the problem is in the add/remove class) 将您的脚本更改为此(问题出在add / remove类中)

 $(document).ready(function (){
   $('.item').mouseover(function () {
      $(this).parent("tr").css("color", "#6eadff");
     }).mouseout(function() {
      $(this).parent("tr").css("color","#000");
   });
 });

why do you do it with JS and not with the css? 为什么你用JS而不用css? ie: 即:

.grid-body table tr:hover {background:red}

Maybe you should just "memorize" the height of the element in a variable when the mouseover event is fired, and then set it back to that value again when the mouseout event is fired. 也许你应该在触发mouseover事件时“记住”变量中元素的高度,然后在触发mouseout事件时再将其设置回该值。

$(document).ready(function (){
 $('.item').mouseover(function () {
     // store the height in a variabile (keep also in mind margins and paddings)
     $(this).parent().addClass("item-over");
 }).mouseout(function() {
     $(this).parent().removeClass("item-over");
     // now set back the original height
 });
 }
);

should work to just add a fixed height to the table row 应该只是为表行添加一个固定的高度

so the containing table row reads: <tr height="50px"> 所以包含的表行为: <tr height="50px">

you can see it working here http://jsfiddle.net/f3TDb/ 你可以看到它在这里工作http://jsfiddle.net/f3TDb/

I'm assuming that you're not doing it wisth divs and css:hover for a specific reason? 我假设你没有这样做,因为div和css:因特定原因而悬停?

i realize i'm months behind on this, but this stumped me yesterday and found this thread. 我意识到我已经落后于此了,但昨天这让我感到难过,并找到了这个帖子。 hopefully my answer helps: 希望我的回答有所帮助:

it's the overflow: auto in div.grid-body that's messing things up. 这是overflow: autodiv.grid-bodyoverflow: auto搞乱了。 you'll have to change this to scroll , possibly: 你必须改变这个以scroll ,可能:

div.grid-body {
    overflow-x: scroll;
}

if you don't want the vertical scrollbars. 如果你不想要垂直滚动条。

note that you'll have to code your js to determine if you need a scrollbar so you can set overflow-x to visible (the default) if there are no overflows and scroll if there are, simulating auto . 请注意,您必须对js进行编码以确定是否需要滚动条,以便在没有溢出时将overflow-x设置为visible (默认值),如果有则scroll ,则模拟auto

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

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