如何使用 jQuery 检测 IE7?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3165489/
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
How to detect IE7 with jQuery?
提问by Mithun Sreedharan
How to detect IE7 with jQuery possibly using jQuery.browser?
如何使用 jQuery.browser 检测带有 jQuery 的 IE7?
回答by Mithun Sreedharan
Got a method
有方法
if ($.browser.msie && parseInt($.browser.version, 10) === 7) {
alert('IE7');
} else {
alert('Non IE7');
}
-- update
- 更新
Please note that $.browseris removed from jQuery 1.9
回答by James Westgate
See $.browser. This feature has been deprecated and you should consider $.supportinstead.
见$.browser。此功能已被弃用,您应该考虑使用$.support代替。
To answer the question, this is a rock solid technique primarily for use in stylesheets but can also be used easily to check browser version, or best still part of a selector.
为了回答这个问题,这是一种坚如磐石的技术,主要用于样式表,但也可以轻松用于检查浏览器版本,或者最好还是选择器的一部分。
Use the following markup (from HTML5 Boilerplate)
<!--[if lt IE 7]><html class="ie6"><![endif]--> <!--[if IE 7]><html class="ie7"><![endif]--> <!--[if IE 8]><html class="ie8"><![endif]--> <!--[if gt IE 8]><!--><html><!--<![endif]--> <head>
Use a selector with hasClass in jQuery
$('html').hasClass('ie7'); $('html.ie7 .myclass').dostuff();
And use as part of a normal selector in css
.mydiv {max-height:50px} .ie6 .mydiv {height:50px} /* ie6 max-height fix */
使用以下标记(来自 HTML5 Boilerplate)
<!--[if lt IE 7]><html class="ie6"><![endif]--> <!--[if IE 7]><html class="ie7"><![endif]--> <!--[if IE 8]><html class="ie8"><![endif]--> <!--[if gt IE 8]><!--><html><!--<![endif]--> <head>
在 jQuery 中使用带有 hasClass 的选择器
$('html').hasClass('ie7'); $('html.ie7 .myclass').dostuff();
并用作 css 中普通选择器的一部分
.mydiv {max-height:50px} .ie6 .mydiv {height:50px} /* ie6 max-height fix */
回答by Nils
All other things considered:
考虑的所有其他事项:
if ($.browser.msie && $.browser.version == 7) {
//do something
};
Should work. Whether or not it is the right way to go about things is another question.
应该管用。这是否是处理事情的正确方式是另一个问题。