Detecting IE6 using jQuery.support

旧城冷巷雨未停 提交于 2019-11-28 03:20:38

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:

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:

ul li:hover, ul li.over { rules here }

You can use a Microsoft Internet Explorer specific Conditional Comment to apply specific code to just IE6.

<!--[if IE 6]>
  Special instructions for IE 6 here... e.g.
  <script>...hook hover event logic here...</script>
<![endif]-->

Thickbox uses

if(typeof document.body.style.maxHeight === "undefined") {
    alert('ie6');
} else {
    alert('other');
}

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 superfish or one of the many alternatives.

If not I suggest you use the jQuery hover() event listener. For example:

$("td").hover(function() {
  $(this).addClass("hover");
}, function() {
  $(this).removeClass("hover");
});

will do what you want.

I would use Whatever:hover - http://www.xs4all.nl/~peterned/csshover.html

It uses behavior and works well for me.

Just for fun (not using jQuery.support):

$(document).ready(function(){
    if(/msie|MSIE 6/.test(navigator.userAgent)){
        alert('OMG im using IE6');
    }
});

You can also do it via php

<?
if(preg_match('/\bmsie 6/i', $ua) && !preg_match('/\bopera/i', $ua)){
    echo 'OMG im using IE6';
}
?>

jQuery.support has no properties to detect :hover support http://docs.jquery.com/Utilities/jQuery.support

But probably you can simply use the hover() event http://docs.jquery.com/Events/hover

if ($.browser.msie && parseInt($.browser.version, 10) == 6) {
  alert("I'm not dead yet!"); 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!