0

SRCIE 8 以下のユーザー向けに iFrameの属性を変更しようとしています。

これが私のコードです:

  <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(これは正しいです)。

私のコードで何が問題になっていますか?

ありがとうございました!

4

4 に答える 4

6

script タグ内に を挿入することはできません。外にある必要があります。

<script>
var i_am_old_ie = false;
</script>

<!--[if lte IE 8]>

<script>
i_am_old_ie = true;
</script>
<![endif]-->
于 2012-08-01T20:33:12.577 に答える
1

As others mentioned you can not use HTML conditional statements between <script> tags.

This should work however:

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

The above will only run in IE8 or below

Edit:

Some nice examples LINK

于 2012-08-01T20:35:17.203 に答える
1

There is a different syntax for conditional comments inside JavaScript. What does @cc_on mean in JavaScript? has details.

Wikipedia's Conditional comment page has an example of version switching using it.

<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>
于 2012-08-01T20:36:02.903 に答える
0

条件付き HTML コメントは、HTML でのみ機能します。

JavaScript は HTML ではありません。

一般的には良い考えではなく、機能検出が推奨されますが、jQuery を使用している場合は次のことができます。

var i_am_old_ie = $.browser.msie && ($.browser.version <= 8);
于 2012-08-01T20:32:49.953 に答える