【问题标题】:How to detect IE7 with javascript or jquery and add a class to div如何使用 javascript 或 jquery 检测 IE7 并将类添加到 div
【发布时间】:2010-09-08 17:02:43
【问题描述】:

有没有办法检测IE7?

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

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

我想改变

<div id="system">

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

提前致谢。

【问题讨论】:

  • 这个问题与样式或 JavaScript 有关吗?如果这是与样式相关的事情,您应该考虑使用建议的条件 cmets。如果这是一个 JavaScript 的东西,你应该阅读特征检测,它在很多方面胜过浏览器嗅探。

标签: javascript jquery internet-explorer-7


【解决方案1】:

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

if (navigator.appVersion.indexOf("MSIE 7.") != -1)
    $('#system').addClass('ie7');

【讨论】:

  • +1 表示不依赖于 javascript 框架或条件 cmets 的可用选项的解决方案。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    您只需使用 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>
    

    如果在 Internet Explorer 7 中执行,这将创建带有类 ie7div。所有其他浏览器和 IE > 7 只会创建没有类的 div。

    【讨论】:

    • 我认为他想要一个 jQuery 解决方案。但是,您的解决方案是一种更好的方法,因为用户可能禁用了 javascript。
    • +1。我通常在 &lt;body class="ie7"&gt; 的一个地方执行此操作,并使用像 body.ie7 #system 这样的 CSS 包含选择器,以避免在整个文档中重复条件 cmets。
    【解决方案4】:

    要检测 ie7 只需使用

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

    用它做你想做的事:

    if($.browser.msie && parseFloat($.browser.version) < 8){
        $('#system').addClass('ie7');
    }
    

    【讨论】:

    • 此时(2 年后),.browser 已从 jQuery 中删除。
    【解决方案5】:

    试试这个:

    <!--[if IE 7]>
    <script type="text/javascript">
      $('#system').addClass('ie7');
    </script>
    <![endif]-->
    

    【讨论】:

      【解决方案6】:
      【解决方案7】:

      请注意,IE8 兼容视图也将 &lt;!--[if IE 7 ]&gt; 设为 true

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-11
        • 1970-01-01
        相关资源
        最近更新 更多