【问题标题】:Conditional comment for IE 8 not workingIE 8 的条件注释不起作用
【发布时间】:2012-08-01 20:31:06
【问题描述】:

我正在尝试为 IE 8 或更低版本的用户更改 iFrame 的 SRC 属性。

这是我的代码:

  <script>
var i_am_old_ie = false;
<!--[if lte IE 8]>
i_am_old_ie = true;
<![endif]-->
</script>
    <script type="text/javascript">
    $(document).ready(function() {
if(i_am_old_ie) {
   $("#shop").attr("src","shopping_cart/browser_support.html"); 
} else {
    $("#shop").attr("src","shopping_cart/index.html"); 
}      
});
</script>

它检测到它是 IE...但仍将所有 IE 用户发送到shopping_cart/browser_support.html。即使我使用 IE 9,它也会将我发送到那里。 7 或 8 也是一样。

但它会将所有其他不使用 IE 的用户发送到 shopping_cart/index.html(这是正确的)。

我的代码有什么问题?

谢谢!

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    你不能在里面插入 script 标签。它需要在外面。

    <script>
    var i_am_old_ie = false;
    </script>
    
    <!--[if lte IE 8]>
    
    <script>
    i_am_old_ie = true;
    </script>
    <![endif]-->
    

    【讨论】:

      【解决方案2】:

      正如其他人提到的,您不能在 &lt;script&gt; 标记之间使用 HTML 条件语句。

      这应该可以工作:

      if (document.all && !document.getElementsByClassName) {
          i_am_old_ie = true;
      }
      

      以上只能在IE8或以下运行

      编辑:

      一些很好的例子LINK

      【讨论】:

        【解决方案3】:

        JavaScript 中的条件 cmets 有不同的语法。 What does @cc_on mean in JavaScript?有详细信息。

        Wikipedia's Conditional comment页面有一个使用它进行版本切换的例子。

        <script>
        /*@cc_on
        
          @if (@_jscript_version == 10)
            document.write("You are using IE10");
        
          @elif (@_jscript_version == 9)
            document.write("You are using IE9");
        
          @elif (@_jscript_version == 5.8)
            document.write("You are using IE8");
        
          @elif (@_jscript_version == 5.7 && window.XMLHttpRequest)
            document.write("You are using IE7");
        
          @elif (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest))
            document.write("You are using IE6");
        
          @elif (@_jscript_version == 5.5)
            document.write("You are using IE5.5");
        
          @else
            document.write("You are using IE5 or older");
        
          @end
        
        @*/
        </script>
        

        【讨论】:

          【解决方案4】:

          条件 HTML cmets 仅适用于 HTML。

          JavaScript 不是 HTML。

          这通常不是一个好主意,推荐特征检测,但如果使用jQuery你可以这样做:

          var i_am_old_ie = $.browser.msie && ($.browser.version <= 8);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-04-02
            • 2012-02-16
            • 1970-01-01
            • 2012-12-08
            • 2014-10-08
            • 2012-05-07
            • 2012-01-25
            • 1970-01-01
            相关资源
            最近更新 更多