Modernizr:如何检测 CSS display:table-cell 支持?

发布于 2024-11-03 07:30:26 字数 219 浏览 5 评论 0原文

我想在支持它的浏览器中使用 display: tabledisplay: table-cell 进行布局。在 IE7 中,我只是想浮动我的列(因为我假设它不可能在该浏览器中工作),但无法找到有关如何使用 Modernizr 执行此操作的任何信息。有人可以帮我吗?

我想我可以使用浏览器条件,但希望不必分解另一个 CSS 文件。

谢谢!

I want to use display: table and display: table-cell for my layout in browsers that support it. In IE7 I simply want to float my columns (since I assume it's not possible to get it to work in that browser), but cannot find anything about how to do it using Modernizr. Can anyone help me please?

I suppose I could use a browser conditional, but was hoping to not have to break out another CSS file.

Thanks!

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

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

发布评论

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

评论(3

昨迟人 2024-11-10 07:30:26

如果您只想对 IE7 及以下版本应用一个不同的规则,我会倾向于不使用 Modernizr 来完成此特定工作。

只需执行类似这样的操作,即可避免“分解另一个 CSS 文件”

#menu li {
    display: table-cell;
    *float: left;
}

这使用 Star Property Hack 提供仅适用于 IE7 及以下版本的规则。

另一种选择是 !ie7 hack,出于某种奇怪的原因,这是我得票最高的答案。

If all you want to do is apply a single different rule for IE7 and less, I'd be tempted not to use Modernizr for this specific job.

Simply do something like this, to avoid having to "break out another CSS file":

#menu li {
    display: table-cell;
    *float: left;
}

This uses the Star Property Hack to provide a rule to only IE7 and below.

Another option is the !ie7 hack, which is for some odd reason my highest voted answer.

绮烟 2024-11-10 07:30:26

...如果您想使用 Modernizr,您可以使用以下代码段:

(function() {
    var displayTests = ["table", "table-caption", "table-cell",
    "table-column", "table-column-group", "table-footer-group",
    "table-header-group", "table-row", "table-row-group"];

    var rules = document.createElement("div").style;

    for (var c=0; c<displayTests.length; c++) {
            var testValue = displayTests[c];
            Modernizr.addTest("display" + testValue, function() {
                    try {
                            rules.display = testValue;
                            return rules.display == testValue;
                    } catch (e) {
                            return false;
                    }
            })
    }
}());

Source [Link]

... And if you want to use Modernizr, you could use this snippet:

(function() {
    var displayTests = ["table", "table-caption", "table-cell",
    "table-column", "table-column-group", "table-footer-group",
    "table-header-group", "table-row", "table-row-group"];

    var rules = document.createElement("div").style;

    for (var c=0; c<displayTests.length; c++) {
            var testValue = displayTests[c];
            Modernizr.addTest("display" + testValue, function() {
                    try {
                            rules.display = testValue;
                            return rules.display == testValue;
                    } catch (e) {
                            return false;
                    }
            })
    }
}());

Source [Link]

雪花飘飘的天空 2024-11-10 07:30:26

当创建的元素是“tfoot”并且显示值为“table-header-group”时,IE 8 不会说实话。即使 IE 8 忽略 CSS 设置并继续在“tbody”下方显示“tfoot”,以下代码片段也不会失败。

try {
    var x = document.createElement('tfoot').style;
    x.display = 'table-header-group';
    console.log('Both IE 8 and Chrome end up here.');
} catch (e) {
    console.log('Except IE 8 should have ended up here, since it does not move the tfoot element.');
}

这可能是“正确的”,因为“tfoot”已经将显示设置为“table-footer-group”;但它是“错误的”,因为它(a)不允许用户覆盖,并且(b)没有告诉用户它不会工作。我没有测试过其他浏览器。

IE 8 does not tell the truth when the created element is a 'tfoot', and the display value is 'table-header-group'. The following snippet will not fail, even though IE 8 ignores the CSS setting and continues to display 'tfoot' below 'tbody'.

try {
    var x = document.createElement('tfoot').style;
    x.display = 'table-header-group';
    console.log('Both IE 8 and Chrome end up here.');
} catch (e) {
    console.log('Except IE 8 should have ended up here, since it does not move the tfoot element.');
}

It might be 'correct', in the sense that 'tfoot' has already set display to 'table-footer-group'; but it's 'wrong' in the sense that it (a) doesn't allow the user to override, and (b) doesn't tell the user that it isn't going to work. I haven't tested other browsers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文