我可以在开始标记中包含IE条件注释吗?

时间:2010-08-20 14:35:42

标签: html internet-explorer conditional-comments

基本上,我想知道这段代码是否合适,

<body id="some_id" <!--[if lt IE 7 ]>class="ie6"<![endif]--> >
</body>

4 个答案:

答案 0 :(得分:6)

没有。 HTML注释不能位于标记内。试试:

<!--[if gte IE 7]>--> <body id="some_id"> <!--<![endif]-->
<!--[if lt IE 7]> <body id="some_id" class="ie6"> <![endif]-->

答案 1 :(得分:2)

不,没有必要。总是将类赋予body元素并将CSS .ie定义保留为除IE6之外的所有浏览器:

.ie6 {
<!--[if lt IE 7]-->
    ... ugly ie6 hack ...
<!--[endif]-->
}
</style>
<body class="ie6">

答案 2 :(得分:1)

否 - 无法在标签内插入评论。

答案 3 :(得分:0)

不,你不能,所以你必须重复标签及其所有属性。

很多人在<html>标记中添加浏览器标识符类。使用body标签也没问题,但我会使用其属性中字符数量最少的标签。

wordpress&#39; body&#39;标签可能看起来像这样

<body class="home page page-id-38 page-template page-template-template-homepage page-template-template-homepage-php woocommerce-demo-store storefront-full-width-content right-sidebar woocommerce-active has-site-logo">

因此,您将该类放在<html>标记中,以便重复该长字符串。

<!DOCTYPE html>
<!--[if lt IE 7 ]> <html class="ie6"<![endif]-->
<!--[if IE 7 ]>    <html class="ie7"<![endif]-->
<!--[if IE 8 ]>    <html class="ie8"<![endif]-->
<!--[if IE 9 ]>    <html class="ie9"<![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html> <!--<![endif]-->
    <head>
    </head>
    <body class="home page page-id-38 page-template page-template-template-homepage page-template-template-homepage-php woocommerce-demo-store storefront-full-width-content right-sidebar woocommerce-active has-site-logo">

html标记可能如下所示。

<html class="" lang="en" xml:lang="en" dir="ltr" prefix="og: http://ogp.me/ns#" itemscope itemtype="http://schema.org/Webpage">

在这种情况下,您可以将该类放在<body>标记中。

<!DOCTYPE html>
<html lang="en" xml:lang="en" dir="ltr" prefix="og: http://ogp.me/ns#" itemscope itemtype="http://schema.org/Webpage">
    <head>
    </head>
    <!--[if lt IE 7 ]> <body class="ie6"><![endif]-->
    <!--[if IE 7 ]>    <body class="ie7"><![endif]-->
    <!--[if IE 8 ]>    <body class="ie8"><![endif]-->
    <!--[if IE 9 ]>    <body class="ie9"><![endif]-->
    <!--[if (gt IE 9)|!(IE)]><!--> <body> <!--<![endif]-->

如果我有什么东西让我没有选择,而是多次重复一个巨大的字符串,就像这样。

<!DOCTYPE html>
<html class="" lang="en" xml:lang="en" dir="ltr" prefix="og: http://ogp.me/ns#" itemscope itemtype="http://schema.org/Webpage">
<head>
</head>
<body class="home page page-id-38 page-template page-template-template-homepage page-template-template-homepage-php woocommerce-demo-store storefront-full-width-content right-sidebar woocommerce-active has-site-logo">

然后我可能会使用javascript

<!DOCTYPE html>
<html class="" lang="en" xml:lang="en" dir="ltr" prefix="og: http://ogp.me/ns#" itemscope itemtype="http://schema.org/Webpage">
<head>
<script>
    (function(){
        var d=document,
            h=d.documentElement,
            v = 3,
            p = d.createElement('p'),
            i = p.getElementsByTagName('i'),
            u;
            while (
                p.innerHTML =  '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
                i[0]
            );
            h.className += (v > 4 ? ' ie'+v : '');
    })()
</script>
</head>
<body class="home page page-id-38 page-template page-template-template-homepage page-template-template-homepage-php woocommerce-demo-store storefront-full-width-content right-sidebar woocommerce-active has-site-logo">

此脚本是James Padolsey's脚本的修改版本,它将类添加到html标记。

相关问题