jQuery if 条件:如果浏览器是 IE 且 IE 浏览器版本低于 9

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4226288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:52:28  来源:igfitidea点击:

if condition: if the browser is IE and IE browser version is older than 9

jqueryinternet-explorerinternet-explorer-7if-statement

提问by laukok

The if condition below I think it says - if the browser is IE and IE browser version is newer than 9, but I don't have IE 9 to test it so it is hard to know the correct output, also this is not 100% of what I want bcos this script should be ran on other browsers too by default like Chrome, Firefox, etc - is it possible to set it in the if condition?

下面的 if 条件我认为它说 - 如果浏览器是 IE 并且 IE 浏览器版本比 9 新,但我没有 IE 9 来测试它所以很难知道正确的输出,这也不是 100%我想要什么 bcos 这个脚本也应该默认在其他浏览器上运行,比如 Chrome、Firefox 等 - 是否可以在 if 条件下设置它?

if ($.browser.msie && parseInt($.browser.version) > 9) 
{
  // run this code
}

The reason why I want to use if condition is that the script appears to have error on IE 7 and of course the best thing is to fix the script but I cannot tell which part of the script that IE doesn't accept it (all other browsers work perfectly fine!). Do u know any tool I can use to debug the script for IE 6, 7, 8, etc? I am using notepad++ to write my jquery, etc, so it doesn't provide any debug stuff...

我想使用 if 条件的原因是脚本在 IE 7 上似乎有错误,当然最好的办法是修复脚本,但我无法确定 IE 不接受脚本的哪一部分(所有其他浏览器工作得很好!)。你知道我可以用什么工具来调试 IE 6、7、8 等的脚本吗?我使用记事本++来编写我的jquery等,所以它不提供任何调试的东西......

So, my next best solution is not to run this script if it is IE browser which is older than 9.

因此,我的下一个最佳解决方案是,如果它是 IE 浏览器的版本超过 9,则不要运行此脚本。

By the way, this is the error message display on the IE7 browser but I can never understand it!

顺便说一句,这是IE7浏览器上显示的错误信息,但我永远无法理解!

Line:910  //which line?
Char:4   // what the hell is this?
Error: Object doesn't support this property or method //what?
Code: 0    // 0 of what?
URL: http://localhost/mysite/page-1 // so which file is causing the error then? the .js or .html or something else??

bloody IE!

该死的IE!

thanks.

谢谢。

采纳答案by Spudley

Firstly, you can get IE9 preview by downloading it from Microsoft's site: http://ie.microsoft.com/testdrive/

首先,您可以通过从 Microsoft 网站下载 IE9 预览版:http: //ie.microsoft.com/testdrive/

Secondly, parseInt($.browser.version) > 9would presumably check that the version is greater than9, which of course it won't be until v10 is released. (you maybe intended >=('greater than or equal to')?

其次,parseInt($.browser.version) > 9大概会检查版本是否大于9,当然在 v10 发布之前不会。(您可能打算>=('大于或等于')?

I usually tell people to avoid browser detection or browser-specific code. There are times when it is necessary, but they're quite rare. Most of the time the developer would be better served by knowing what was failing and working around it (tools like Modernizrreally help for this sort of thing).

我通常告诉人们避免浏览器检测或特定于浏览器的代码。有时需要这样做,但这种情况很少见。大多数情况下,通过了解失败的原因并解决它,开发人员会得到更好的服务(像Modernizr这样的工具确实对这类事情有帮助)。

However there are times when one simply has to do browser detection.

然而,有时人们只需要进行浏览器检测。

In your case, if you really need to detect IE, don't do it the way you're doing (ie checking for version 9); it'd be better to check for older versions, and I'd suggest that a conditional comment would be the best way to do it.

在您的情况下,如果您确实需要检测 IE,请不要按照您的方式进行(即检查版本 9);最好检查旧版本,我建议有条件的评论是最好的方法。

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

Then your if()statement later on can just look like this:

那么你if()稍后的声明可以是这样的:

if(i_am_old_ie) {
   //do stuff for IE6/7/8
} else {
   //do stuff for all other browsers (including IE9)
}

回答by Harmen

Your code looks fine, but you forgot to set the radix parameter in parseInt:

您的代码看起来不错,但是您忘记在 中设置基数参数parseInt

if ($.browser.msie && parseInt($.browser.version, 10) > 9){

  // you need to wait a couple years to test if it works...
  alert("I'm IE10 or 11...");

}

This cannot cause any errors

这不会导致任何错误

回答by Endre Simo

Here is the Javascript version to test if the browser is IE:

这是用于测试浏览器是否为 IE 的 Javascript 版本:

<script>

    function getIEVersion() {
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.test(ua) != null)
                rv = parseFloat( RegExp. );
        }
        return rv;
    }


    function checkVersion() {
        var ver = getIEVersion();

        if ( ver != -1 ) {
            if (ver <= 9.0) {
                // do something
            }
        }
    }

    checkVersion();

</script>

This method is useful in case You are using HTML5 specific modules and want to implement some fallback functions in case the browser not supporting those features, for ex. <canvas>

如果您正在使用 HTML5 特定模块并希望在浏览器不支持这些功能的情况下实现一些后备功能,则此方法很有用,例如。 <canvas>

回答by t.mikael.d

There's an even easier method.

还有一个更简单的方法。

Since this is a comment, it will be ignored by all other browsers except IE.

由于这是一个注释,它会被除 IE 之外的所有其他浏览器忽略。

var IE;
//@cc_on IE = navigator.appVersion;

Then just use this in you'r script to detect all IE versions.

然后只需在您的脚本中使用它来检测所有 IE 版本。

if (IE) {
//This is IE.
}
else {
//This is NOT IE.
}

Or match against any version like this:

或者匹配任何这样的版本:

if (IE <= 6) {}, if (IE < 9) }, if (IE == 7) {} etc etc...

Here's the source where i found these types of conditionals: http://www.javascriptkit.com/javatutors/conditionalcompile.shtml

这是我发现这些类型条件的来源:http: //www.javascriptkit.com/javatutors/conditionalcompile.shtml

回答by user3101105

Instead of putting in a bunch of inline javascript you could just add an empty div for the version of ie you are targeting, so put the following at the top of your html, just inside the body tag:

而不是放入一堆内联javascript,你可以只为你正在定位的ie版本添加一个空的div,所以将以下内容放在你的html顶部,就在body标签内:

<!--[if LT IE  9]>
    <div id="iedetect" class="oldie"></div>
<![endif]-->

Then you can place an if statement in your jquery to check for that block.

然后你可以在你的 jquery 中放置一个 if 语句来检查那个块。

if ($("#iedetect").is(".oldie")) {
    //Do something
} else {
   //Do Something else in all other browsers
}

回答by user3101105

<!--[if IE 8]>
  <script type="text/javascript">
    // this is only executed for IE 8
  </script>
<![endif]-->

回答by user3101105

or you could do this

或者你可以这样做

<!--[if IE 9]>
  <script type="text/javascript">
    // this is only executed for IE 9
  </script>
<![endif]-->

回答by Timothy

I found cascading it works great for multibrowser detection. This code was used to change a fade to show/hide in ie 8 7 6.

我发现级联它非常适合多浏览器检测。此代码用于更改淡入淡出以显示/隐藏即 8 7 6。

$(document).ready(function(){
    if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 8.0)
         { 
             $(".glow").hide();
            $('#shop').hover(function() {
        $(".glow").show();
    }, function() {
        $(".glow").hide();
    });
         }
         else
         { if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 7.0)
         { 
             $(".glow").hide();
            $('#shop').hover(function() {
        $(".glow").show();
    }, function() {
        $(".glow").hide();
    });
         }
         else
         {if(jQuery.browser.msie && jQuery.browser.version.substring(0, 1) == 6.0)
         { 
             $(".glow").hide();
            $('#shop').hover(function() {
        $(".glow").show();
    }, function() {
        $(".glow").hide();
    });
         }
         else
         { $('#shop').hover(function() {
        $(".glow").stop(true).fadeTo("400ms", 1);
    }, function() {
        $(".glow").stop(true).fadeTo("400ms", 0.2);});
         }
         }
         }
       });

回答by Gregg B

<!--[if (IE 9)|!(IE)]><!--> Script here <!--<![endif]-->