【问题标题】:Chrome conditional commentsChrome 条件注释
【发布时间】:2010-11-20 12:08:44
【问题描述】:

Chrome 是否有条件 cmets 之类的东西?

与 Firefox 相比,我有一个在 Chrome 中呈现不同的页面。

谢谢

【问题讨论】:

  • 如果您可以指定您的问题,那么它可能会更有帮助。

标签: css firefox google-chrome


【解决方案1】:

您可以在您的 CSS 中使用它来定位基于 WebKit 的浏览器

@media screen and (-webkit-min-device-pixel-ratio:0) { 

Body {}

}

也许这会有所帮助?

【讨论】:

  • 非 webkit 浏览器没有 -webkit-min-device-pixel-ratio(为零或其他),因此条件的第二部分失败。
  • 这对我有用!但是我还有一个问题,如果这对某些专家来说听起来很愚蠢,请原谅,但是其他浏览器在获得更新后是否会考虑这个问题?前火狐?谢谢
  • +1 来自我。 @Mbarry:firefox 永远不会采用其中包含“webkit”的 css 属性。这些 CSS 供应商前缀目前设置得很清楚,未来肯定是开放的,有些人会在几年前争论放弃它们,改为“beta”前缀作为实例。不管怎样,他们现在就在这里。
【解决方案2】:
<!--[if IE 8]><div id="bodyContainer" class="IE8"><![endif]-->
<!--[if !IE]>--><div id="bodyContainer" class="W3C"><!--<![endif]-->
<script type="text/javascript"> 
    if (navigator.userAgent.toLowerCase().match('chrome') && document.getElementById('bodyContainer'))
        document.getElementById('bodyContainer').className = document.getElementById('bodyContainer').className + " chrome";
</script>

然后您使用 CSS 专门针对 Chrome 调整样式。

【讨论】:

  • document.getElementById('bodyContainer').className += " chrome";
【解决方案3】:

据我所知,只有 Internet Explorer 4-8 支持 HTML conditional comments 和 Javascript conditional compilation directives

【讨论】:

    【解决方案4】:
    <!--[if !IE]>-->
    

    这不仅仅是一个 Chrome 条件注释 - 这会影响所有不是 IE 的浏览器... 火狐,野生动物园等 如果使用 PHP - 试试这个:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Browsser Detection</title>
    
    <link rel="stylesheet" href="Main.css" type="text/css">
    
    <?php 
    
    $msie        = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE') ? true : false; 
    $firefox    = strpos($_SERVER["HTTP_USER_AGENT"], 'Firefox') ? true : false;
    $safari        = strpos($_SERVER["HTTP_USER_AGENT"], 'Safari') ? true : false;
    $chrome        = strpos($_SERVER["HTTP_USER_AGENT"], 'Chrome') ? true : false;
    
    if ($msie) {
    echo '
    <!--[if IE 7]>
    <link rel="stylesheet" href="ie7.css" type="text/css">
    <![endif]-->
    <!--[if IE 8]>
    <link rel="stylesheet" href="ie8.css" type="text/css">
    <![endif]-->
    ';
    }
    if ($safari) {
    echo '<link rel="stylesheet" href="safari.css" type="text/css">';
    }
    
    ?>
    
    </head>
    <body>
    
        <br>
        <?php
        if ($firefox) { //Firefox?
        echo 'you are using Firefox!';
        }
    
        if ($safari || $chrome) { // Safari?
        echo 'you are using a webkit powered browser';
        }
    
        if (!$msie) { // Not IE?
        echo '<br>you are not using Internet Explorer<br>';
        }
        if ($msie) { // IE?
        echo '<br>you are using Internet Explorer<br>';
        }
        ?>
    
        <br>
    
    </body>
    </html>
    

    感谢 John 发帖:http://www.killersites.com/forums/topic/2731/firefox-google-chrome-browser-detecion-using-conditional-comments-hack/

    【讨论】:

      【解决方案5】:

      条件操作将起作用,因为其他浏览器会将 If IE8 块解析为 HTML 注释,而不是 !IE 块,因为它的内部包含在 --> 和

      因此,对于所有非 IE 浏览器,body 类确实等于 W3C。

      不过,这都是顺便说一句,因为不需要 IE 注释块来将浏览器专门识别为 chrome - JS 块本身会做到这一点,前提是用户当然打开了 JS。

      【讨论】:

      • 关掉 JS 的用户太少了,不用管 :-)
      • 我不同意。像 NoScript for Firefox 这样的附加组件比您想象的要普遍得多。
      • 常见于技术负责人和开发人员,而不是妈妈、爸爸和使用网络的普通人。
      猜你喜欢
      • 2012-04-26
      • 2011-03-03
      • 2011-10-23
      • 2015-10-21
      • 2011-02-04
      • 2012-08-08
      • 2017-03-27
      • 2016-07-22
      • 2013-06-23
      相关资源
      最近更新 更多