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

标签 javascript jquery internet-explorer internet-explorer-9

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

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

但我想要的是在 scipt 标签内检查这个条件并使用 jQuery document.ready

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

最佳答案

您可以通过 <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]-->

... 然后检查是否 <html>有课.lt9.lt8 — 无论您的目标是什么版本的 Internet Explorer:

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

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

关于javascript - 如果浏览器 lt ie9 执行某些 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18145816/

相关文章:

javascript - 为什么我的箭头函数不返回数组而是函数声明

javascript - Bootstrap 单击后触发 jQuery click(),导致冲突

python - 通过 ajax 调用 Flask 更改来自另一个驱动器的图像源

Jquery ajax,当定期调用时,在 IE8 上不起作用

javascript - try..catch 未捕获异步/等待错误

javascript - 使用 React Navigation 访问 Redux 状态

javascript - 如何按顺序获取所有元素文本

javascript - 如何使用 ajax 从下拉菜单更新 mysql 数据库

css - 在 IE 中对 css 生成的内容应用文本装饰

html - IE7、8、9 无法正确读取媒体查询。