如何使用 jQuery.support 检测 IE7 和 IE8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8890460/
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 and IE8 using jQuery.support
提问by Mithun Sreedharan
How can I detect IE 7 and IE 8 using jQuery.supportproperties?
如何使用jQuery.support属性检测 IE 7 和 IE 8 ?
Is it possible to detect the browser versions using jQuery.support or just those blah blah blah browser features?
是否可以使用 jQuery.support 或仅使用那些 blah blah blah 浏览器功能来检测浏览器版本?
回答by jas
This is totally possible with support:
这在支持下是完全可能的:
if (!$.support.leadingWhitespace) {
//IE7 and 8 stuff
}
This also detects IE 6 however, if you don't want IE 6 to run this code block, you will need another flag to exclude it
这也会检测 IE 6 但是,如果您不希望 IE 6 运行此代码块,则需要另一个标志来排除它
A good reason for not using browser is it is a deprecated feature, and it will likely be removed to a plugin with version 1.9. (See the comments on the answer to How to detect IE7 with jQuery?)
不使用浏览器的一个很好的理由是它是一个已弃用的功能,它可能会被删除到 1.9 版的插件中。(请参阅对如何使用 jQuery 检测 IE7?的答案的评论?)
"We recommend against using this property; please try to use feature detection instead (see jQuery.support). jQuery.browser may be moved to a plugin in a future release of jQuery" http://api.jquery.com/jQuery.browser/
“我们建议不要使用此属性;请尝试改用功能检测(请参阅 jQuery.support)。jQuery.browser 可能会在 jQuery 的未来版本中移动到插件中” http://api.jquery.com/jQuery。浏览器/
here is a working example: http://jsfiddle.net/AGtG8/16/
这是一个工作示例:http: //jsfiddle.net/AGtG8/16/
回答by piciu
if (jQuery.support.leadingWhitespace == false){
... code for IE7-IE8 ...
}
else {
...
}
回答by Bob Hamblok
Be careful with the following because it also includes IE10:
请注意以下事项,因为它还包括 IE10:
if ($.browser.msie && $.browser.version.substr(0,1)<7) {
//<IE7
}
better use:
更好地使用:
if ($.browser.msie && parseInt($.browser.version,10)<7) {
//<IE7
}
回答by SequenceDigitale.com
One trick I found is to add Conditionnal Tags in the HTML
我发现的一个技巧是在 HTML 中添加条件标签
<!--[if lt IE 7]><body class="ie ie6 lte9 lte8 lte7"><![endif]-->
<!--[if IE 7]><body class="ie ie7 lte9 lte8 lte7"><![endif]-->
<!--[if IE 8]><body class="ie ie8 lte9 lte8"><![endif]-->
<!--[if IE 9]><body class="ie ie9 lte9"><![endif]-->
<!--[if !IE]><!--><body class="not-ie"><!--<![endif]-->
Then use JQuery like this :
然后像这样使用 JQuery:
$('body.ie7')
It also helps for CSS on IE specific
它还有助于特定于 IE 的 CSS
body.ie6{ margin: 0; }
I don't know if it's good for you but anyways, that's still an option.
我不知道这对你是否有好处,但无论如何,这仍然是一个选择。
回答by AndreyM
jQuery.support is for detecting browser features. To detect browser version use jQuery.browser:
jQuery.support 用于检测浏览器功能。要检测浏览器版本,请使用 jQuery.browser:
if ($.browser.msie && $.browser.version.substr(0,1)<7) {
//IE7
}
Update: $.browser is deprecated now, don't use it.
更新:$.browser 现在已弃用,请勿使用。
回答by oezi
as explained, .support
is for feature-detection. if you want to detect the browser, just use .browser
.
如前所述,.support
用于特征检测。如果要检测浏览器,只需使用.browser
.
var ua = $.browser;
if ( ua.msie && ua.version.slice(0,1) == "8" ) {
alert('IE 8');
} else if ( ua.msie && ua.version.slice(0,1) == "7" ) {
alert('IE 7');
} else {
alert('something else');
}
回答by Shannon Hochkins
I've noticed many different ways of doing this, this DOESN'T use $.support
although I've found that this works very well. Also just as a note, jquery has just announced that they will be removing any support for IE 6,7,8 in jquery 2.0 (http://blog.jquery.com/2012/06/28/jquery-core-version-1-9-and-beyond/).
我注意到有很多不同的方法来做到这一点,$.support
虽然我发现这很好用,但它并没有使用。同样作为说明,jquery 刚刚宣布他们将在 jquery 2.0 ( http://blog.jquery.com/2012/06/28/jquery-core-version- 1-9 及以上/)。
var msVersion = navigator.userAgent.match(/MSIE ([0-9]{1,}[\.0-9]{0,})/),
msie = !!msVersion,
ie6 = msie && parseFloat(msVersion[1]) < 7;
// Help prevent flashes of unstyled content
if (!ie6) {
//Do Something here.
}
Happy coding!
快乐编码!
回答by Dan
I use this to check if browser is ie 8 or less
我用它来检查浏览器是否 ie 8 或更少
to change the ie version simply update the 8 to another ie verion
要更改 ie 版本,只需将 8 更新为另一个 ie 版本
if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) <= 8 ) {
runIECode();
} else {
runOther();
}
回答by coorasse
I added few lines of Javascript to make it work again:
我添加了几行 Javascript 使其再次工作:
jQuery.uaMatch = function (ua) {
ua = ua.toLowerCase();
var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
/(webkit)[ \/]([\w.]+)/.exec(ua) ||
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
/(msie) ([\w.]+)/.exec(ua) ||
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
[];
return {
browser: match[ 1 ] || "",
version: match[ 2 ] || "0"
};
};
if (!jQuery.browser) {
matched = jQuery.uaMatch(navigator.userAgent);
browser = {};
if (matched.browser) {
browser[ matched.browser ] = true;
browser.version = matched.version;
}
// Chrome is Webkit, but Webkit is also Safari.
if (browser.chrome) {
browser.webkit = true;
} else if (browser.webkit) {
browser.safari = true;
}
jQuery.browser = browser;
}
回答by madhi
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
var ieversion = new Number(RegExp.);
alert(ieversion < 9);
}