开发者

How do I get ".is" to work in older versions of Internet Explorer

开发者 https://www.devze.com 2023-04-09 08:31 出处:网络
I\'m not getting \".is\" to work. It\'s working great in IE9, firefox and other modern browsers but not IE8 or IE7

I'm not getting ".is" to work. It's working great in IE9, firefox and other modern browsers but not IE8 or IE7

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
</head>

<body>

<script type="text/javascript">
        $('body').click(function () {                   
                if (!$('.myDiv').is(":hover")) {
                    alert("outside of blue div");
                }
        });
</script>

<div class="m开发者_StackOverflow社区yDiv" style="width:150px; height:150px; background:blue">&nbsp;</div>

</body>

</html>

Or look here http://jsfiddle.net/uh8RB/

How do I get it to work in IE8 and older?


Try using $(document).click instead of $('body').click. Sometimes the <body> tag doesn't take up the full screen height/width, so you may be clicking outside the <body> tag.

Also, instead of .is(":hover"), try !$(e.target).is('.myDiv') $(e.target).closest('.myDiv').length === 0 (.closest is used to detect if we are clicking the div or a child of the div). I don't think IE7 or IE8 support the :hover CSS pseudo-property.

$(document).click(function(e) {                   
   if($(e.target).closest('.myDiv').length === 0) {
      alert("outside of blue div");
   }
});

Demo: http://jsfiddle.net/uh8RB/5/
More Tests: http://jsfiddle.net/uh8RB/8/
Updated Demo: http://jsfiddle.net/uh8RB/14/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号