如果浏览器 lt ie9,则执行某些 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18145816/
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-10-27 10:52:48  来源:igfitidea点击:

Execute certain javascript if browser lt ie9

javascriptjqueryinternet-explorerinternet-explorer-9

提问by melvindidit

I want to execute a certain code in jQuery if the browser is lower than IE9. And yes, i already know about

如果浏览器低于IE9,我想在jQuery中执行某些代码。是的,我已经知道了

<!--[if ... IE ]> <![endif]-->

But what i want is to check this condition within the scipt tag and with jQuery document.ready

但我想要的是在 scipt 标签和 jQuery document.ready 中检查这个条件

<script>
    $(document).ready(function(){
        //code to check if lt ie9 
    });
</script>

回答by Daniel

You can target older Internet Explorer versions (<= 9) through classes on the <html>element...

您可以通过<html>元素上的类来定位较旧的 Internet Explorer 版本 (<= 9) ...

<!--[if lt IE 7]>  <html class="ie ie6 lte9 lte8 lte7"> <![endif]-->
<!--[if IE 7]>     <html class="ie ie7 lte9 lte8 lte7"> <![endif]-->
<!--[if IE 8]>     <html class="ie ie8 lte9 lte8"> <![endif]-->
<!--[if IE 9]>     <html class="ie ie9 lte9"> <![endif]-->
<!--[if gt IE 9]>  <html> <![endif]-->
<!--[if !IE]><!--> <html>             <!--<![endif]-->

... and then check if <html>has the class .lt9or .lt8— whatever version of Internet Explorer you are targeting:

...然后检查是否<html>有类.lt9.lt8- 您所针对的任何版本的 Internet Explorer:

if($('html').hasClass('lte9')) { /* LTE IE9 */ }

However, I would suggest using Modernizrfeature detection instead of checking for a specific browser.

但是,我建议使用Modernizr功能检测而不是检查特定浏览器。

回答by Aegis

Use feature detection instead of browser detection. i.e. http://modernizr.com/

使用特征检测而不是浏览器检测。即http://modernizr.com/