通过 CSS 隐藏的 IE7 表格单元格不能通过后续的类更改变得可见 (??)

发布于 2024-09-06 11:08:33 字数 1163 浏览 2 评论 0原文

这是两个测试文件:

http://gutfullofbeer.net/good-table.html

http://gutfullofbeer.net/bad-table.html

这两个页面上的标记几乎都是相同的。有一个有两列的表。一列(第二列)的 元素都被赋予类“junk”。

在“良好”页面中,当您加载它时,您会在顶部看到一个未选中的复选框。如果选中该复选框,第二列就会消失。如果取消选中它,则会返回第二列。在“坏”页面中,复选框开始处于选中状态。取消选中它在 IE7 中没有任何效果,但它可以在其他没有被根本邪恶所控制的浏览器中工作。

该复选框与一个小的 Javascript 例程挂钩,该例程仅在 标记中添加或删除“compact”类。有一个样式表包含以下内容:

table.compact th.junk, table.compact td.junk {
  display: none;
}

因此,应该发生的事情就是“好”页面上发生的事情。然而,在 IE7(也可能是 6)中,如果表格元素开始使得当最初渲染时它们被设置为不可见,则它们将永远不会被看到,无论 DOM 的后续更改如何,都会使新的样式规则生效并使其可见。 (这似乎是 部分的问题;我在其他元素上使用相同的机制,并且它们都工作正常。)

所以,问题是:有人知道吗一些黑客——无论多么令人反感——可以用来绕过这种愚蠢的行为?显然,我可以尝试安排 IE7 使用相关的切换控件集启动其视图,以便表格单元格可见,但在我的情况下,这种情况发生在作为 AJAX 响应生成的表格周围,因此这将是一个很大的问题我宁愿避免混乱。 (该表格实际上也是一个表格;它是表格信息的显示,而不是布局黑客。)

我用谷歌搜索了一下,没有找到任何东西,如果你考虑一下你从“IE7”获得了多少点击,这就不奇怪了。布局错误”搜索。

Here are two test files:

http://gutfullofbeer.net/good-table.html

http://gutfullofbeer.net/bad-table.html

The markup on those two page is all almost the same. There's a table with two columns. The <th> and <td> elements of one column (the second one) are all given the class "junk".

In the "good" page, when you load it you'll see a checkbox unchecked at the top. If you check the checkbox, the second column should disappear. If you uncheck it, the second column comes back. In the "bad" page, the checkbox starts out checked. Unchecking it has no effect in IE7, though it works in other browsers that are not possessed by fundamental evil.

The checkbox is hooked to a little Javascript routine that just adds or removes the class "compact" from the <table> tag. There's a stylesheet that includes this:

table.compact th.junk, table.compact td.junk {
  display: none;
}

Thus what should happen is what happens on the "good" page. However, it appears that in IE7 (maybe 6 too) if table elements start out such that when initially rendered they're styled to be invisible, they'll never be seen, regardless of subsequent changes to the DOM that would bring new style rules into effect and leave them visible. (This appears be an issue with <table> parts in particular; I'm using the same mechanism elsewhere with other elements and they all work fine.)

So, the question is: does anybody know of some hack — however revolting — that can be used to get around this idiotic behavior? Obviously I could try and arrange for IE7 to start its views with the relevant toggle control set so that the table cells are visible, but in my case this happens around a table that's produced as an AJAX response, and so it'd be a big mess I'd rather avoid. (The table is really a table, too; it's a display of tabular information, not a layout hack.)

I've googled around and not found anything, which shouldn't be surprising if you consider how many hits you get from "IE7 layout bug" searches.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风念你 2024-09-13 11:08:33

我没有安装 IE 7,但 IE 6 也有同样的问题。以下是我修复该问题的方法:

$(function() {
  $('#click').click(function() {
    $(".compact th+th,.compact td+td").toggleClass('junk',this.checked);
  });
});

问题出在您的选择器上。切换compact不会增加junk的可见性。

I don't have IE 7 installed, but it was the same issue with IE 6. Here's what I did to fix it:

$(function() {
  $('#click').click(function() {
    $(".compact th+th,.compact td+td").toggleClass('junk',this.checked);
  });
});

The problem was with you selector. Toggling on compact would not add visibility to junk.

自由如风 2024-09-13 11:08:33

这是一个渲染错误。 IE6/7 不使用正确的表格显示模型。不幸的是,我记不起这个特定错误的具体名称/标签,而且我似乎找不到权威资源来证实这一点。

至少,我找到了 2 个 CSS 方法来解决这个问题。

  1. 最简单的方法是使用 visibility:hidden; 而不是 display:none;。不幸的是,如果您在要切换的列之后有更多列或有表格边框,则此方法效果不佳。它仍然留有空间。将 position:absolute; 添加到 .junk 可以修复 FF 中的此问题,但在 IE 中又会遇到相同的渲染问题。

  2. 滥用 IE 的错误能力来应用 样式的黑客行为。

    ;
    
        <头>
            <脚本 src="http://code.jquery.com/jquery-latest.min.js">
            <脚本>
                $(函数() {
                    $('#click').click(function() {
                        $('table').toggleClass('compact', this.checked);
                    });
                });
            
            <风格>
                table.compact.junk { 显示:无; }
            
            
        
        <正文>
            <输入类型=“复选框”id=“点击”选中>
            <表类=“紧凑”>
                >
                
                
                    你好
                    世界
                
                
                    Foo
                    酒吧
                
                
                    Foo
                    酒吧
                
            
        
    
    

或者,您也可以只切换 jQuery 中实际 感兴趣的元素:

$(function() {
    $('#click').click(function() {
        $('table.compact .junk').toggle(!this.checked);
    });
});

This is a rendering bug. IE6/7 doesn't use a proper table display model. Unfortunately I can't recall a specific name/label for this particular bug and I can't seem to find authorative resources confirming this.

At least, I found 2 CSS ways to fix this.

  1. The easiest, use visibility: hidden; instead of display: none;. Unfortunately this doesn't work nicely if you have more columns after the to-be-toggled column or have a table border. It still leaves a space. Adding position: absolute; to .junk fixes this issue in FF, but you fall back to the same rendering problem in IE.

  2. A hack which abuses the IE's erroneous ability to apply styles for <col>.

    <!DOCTYPE html>
    <html>
        <head>
            <script src="http://code.jquery.com/jquery-latest.min.js"></script>
            <script>
                $(function() {
                    $('#click').click(function() {
                        $('table').toggleClass('compact', this.checked);
                    });
                });
            </script>
            <style>
                table.compact .junk { display: none; }
            </style>
            <!--[if lte IE 7]>
            <style>
                table.compact .junk { display: block; }
                table.compact col.junk { display: none; }
            </style>
            <![endif]-->
        </head>
        <body>
            <input type="checkbox" id="click" checked>
            <table class="compact">
                <col />
                <col class="junk" />
                <tr>
                    <th>Hello</th>
                    <th class="junk">World</th>
                </tr>
                <tr>
                    <td>Foo</td>
                    <td class="junk">Bar</td>
                </tr>
                <tr>
                    <td>Foo</td>
                    <td class="junk">Bar</td>
                </tr>
            </table>
        </body>
    </html>
    

Alternatively, you can also just toggle the elements of actual interest in jQuery:

$(function() {
    $('#click').click(function() {
        $('table.compact .junk').toggle(!this.checked);
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文