使用 jQuery.support 检测 IE6
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/584285/
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
Detecting IE6 using jQuery.support
提问by Jaime
Anyone have any ideas on how to test for something specific for IE6 (and not IE7) using jquery.support?
任何人对如何使用 jquery.support 测试特定于 IE6(而不是 IE7)的东西有任何想法吗?
My problem is that IE6 supports :hover psuedo-class only for anchor elements and IE7 does support it for all elements (as FF, Chrome, etc). So I want to do something special in case the browser does not support :hover for all elements... thus I need a way to test for this feature. (don't want to use jQuery.browser). Any ideas?
我的问题是 IE6 仅支持 :hover psuedo-class 锚元素,而 IE7 确实支持所有元素(如 FF、Chrome 等)。所以我想做一些特别的事情,以防浏览器不支持 :hover 所有元素......因此我需要一种方法来测试这个功能。(不想使用 jQuery.browser)。有任何想法吗?
回答by FriendOfFuture
While it's good practice to check for feature support rather then user agent, there's no simple way to check for something like support of a css property using JavaScript. I recommend you either follow the above posters suggestion of using conditional comments or use jQuery.browser. A simple implementation (not validated for performance or bugs) could look like this:
虽然检查功能支持而不是用户代理是一种很好的做法,但没有简单的方法来检查诸如使用 JavaScript 的 css 属性支持之类的东西。我建议您按照上述海报建议使用条件注释或使用 jQuery.browser。一个简单的实现(未验证性能或错误)可能如下所示:
if ($.browser.msie && $.browser.version.substr(0,1)<7) {
// search for selectors you want to add hover behavior to
$('.jshover').hover(
function() {
$(this).addClass('over');
},
function() {
$(this).removeClass('over');
}
}
In your markup, add the .jshover class to any element you want hover css effects on. In your css, add rules like this:
在您的标记中,将 .jshover 类添加到您想要悬停 css 效果的任何元素。在您的 css 中,添加如下规则:
ul li:hover, ul li.over { rules here }
回答by scunliffe
You can use a Microsoft Internet Explorer specific Conditional Commentto apply specific code to just IE6.
您可以使用 Microsoft Internet Explorer 特定的条件注释将特定代码仅应用于 IE6。
<!--[if IE 6]>
Special instructions for IE 6 here... e.g.
<script>...hook hover event logic here...</script>
<![endif]-->
回答by Andrew
Thickbox uses
厚盒用途
if(typeof document.body.style.maxHeight === "undefined") {
alert('ie6');
} else {
alert('other');
}
回答by cletus
This is one example of where we should take a step back and ask why you're doing that.
这是我们应该退后一步并询问您为什么这样做的一个示例。
Typically it's to create a menu. If so I highly suggest you save yourself some headaches and use a plug-in like superfishor one of the many alternatives.
通常是创建一个菜单。如果是这样,我强烈建议您省去一些麻烦,并使用像superfish这样的插件或众多替代品之一。
If not I suggest you use the jQuery hover()event listener. For example:
如果不是,我建议您使用jQuery hover()事件侦听器。例如:
$("td").hover(function() {
$(this).addClass("hover");
}, function() {
$(this).removeClass("hover");
});
will do what you want.
会做你想做的。
回答by cletus
I would use Whatever:hover - http://www.xs4all.nl/~peterned/csshover.html
我会使用什么:悬停 - http://www.xs4all.nl/~peterned/csshover.html
It uses behavior and works well for me.
它使用行为并且对我来说效果很好。
回答by Andres SK
Just for fun (not using jQuery.support):
只是为了好玩(不使用 jQuery.support):
$(document).ready(function(){
if(/msie|MSIE 6/.test(navigator.userAgent)){
alert('OMG im using IE6');
}
});
You can also do it via php
你也可以通过php来做
<?
if(preg_match('/\bmsie 6/i', $ua) && !preg_match('/\bopera/i', $ua)){
echo 'OMG im using IE6';
}
?>
回答by alex2k8
jQuery.support has no properties to detect :hover support http://docs.jquery.com/Utilities/jQuery.support
jQuery.support 没有检测属性:悬停支持 http://docs.jquery.com/Utilities/jQuery.support
But probably you can simply use the hover() event http://docs.jquery.com/Events/hover
但也许你可以简单地使用 hover() 事件 http://docs.jquery.com/Events/hover
回答by Mithun Sreedharan
if ($.browser.msie && parseInt($.browser.version, 10) == 6) {
alert("I'm not dead yet!");
}