代码之家  ›  专栏  ›  技术社区  ›  shin

如何使用javascript或jquery检测IE7并向DIV添加类

  •  15
  • shin  · 技术社区  · 15 年前

    有没有办法检测到IE7?

    IE8中的代码没有任何问题,但IE7有问题。

    所以我想我可以用jquery添加一个类,当浏览器IE7通过javascript检测到它时。

    我想换成

    <div id="system">
    

    <div id="system" class="ie7">
    

    事先谢谢。

    7 回复  |  直到 11 年前
        1
  •  10
  •   Jimmy    15 年前

    您可以使用IE条件注释通过javascript添加类,如下所示:

    <!--[if IE 7]>
    <script type="text/javascript">
    $(document).ready(function() {
        $('#system').addClass('ie7');
    });
    </script>
    <![endif]-->
    
        2
  •  50
  •   Alex    15 年前

    如果您真的想通过使用javascript来解决这个问题,您可能需要检查如下版本:

    if (navigator.appVersion.indexOf("MSIE 7.") != -1)
        $('#system').addClass('ie7');
    
        3
  •  9
  •   jAndy    15 年前

    您可以只使用HTML:

    CSS

    .ie7{
       padding: 0;
       color: red;
    }
    

    HTML

    <!--[if IE 7 ]> <div id="system" class="ie7"> <![endif]-->
    <!--[if (gt IE 7)|!(IE)]><!--> <div id="system"> <!--<![endif]-->
    </div>
    

    这就产生了 div 随班就读 ie7 如果在Internet Explorer 7中执行。所有其他浏览器和IE>7只会创建没有类的DIV。

        4
  •  9
  •   Boro    14 年前

    检测IE7只需使用

    if($.browser.msie && parseFloat($.browser.version) < 8){
        //do other stuff
        return;
    }
    

    只需:

    if($.browser.msie && parseFloat($.browser.version) < 8){
        $('#system').addClass('ie7');
    }
    
        5
  •  3
  •   Enlightened    15 年前

    试试这个:

    <!--[if IE 7]>
    <script type="text/javascript">
      $('#system').addClass('ie7');
    </script>
    <![endif]-->
    
        7
  •  0
  •   Vignesh Subramanian    11 年前

    请注意,IE8兼容视图也将 <!--[if IE 7 ]> 为真

    因此,如果不希望更改反映在IE8兼容视图中,您应该对文档模式进行二级测试。

    <!--[if IE 7]>
        <script type="text/javascript"> 
       try
        {
        if(document.documentMode!=8){
         //Your code comes here
        }
        }
        catch(exception){
        }
       </script>
    <!--<![endif]-->