最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery mousedown event not firing for window in IE8 - Stack Overflow

programmeradmin1浏览0评论

I'm having problems hooking up the mousedown event for the window using jQuery in IE8. I'm getting no errors, but the event does not seem to be firing. It does work in IE9 and all other browsers I have tried. Here's my code:

<html>
<head>
    <script src=".7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function test(e) {
            alert('test');
        }

        $(document).ready(function () {
            $(window).mousedown(test);
        });     
    </script>   
</head>
<body>   
</body>
</html>

I'm having problems hooking up the mousedown event for the window using jQuery in IE8. I'm getting no errors, but the event does not seem to be firing. It does work in IE9 and all other browsers I have tried. Here's my code:

<html>
<head>
    <script src="https://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function test(e) {
            alert('test');
        }

        $(document).ready(function () {
            $(window).mousedown(test);
        });     
    </script>   
</head>
<body>   
</body>
</html>
Share Improve this question edited May 22, 2020 at 17:08 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 7, 2011 at 10:38 NocklasNocklas 1,3675 gold badges15 silver badges29 bronze badges 1
  • Could you just use .click() instead to escape inconsistent mousedown handling (especially in Opera and IE)? – Interrobang Commented Dec 7, 2011 at 10:43
Add a ment  | 

2 Answers 2

Reset to default 5

use document instead of window

$(document).ready(function() {
    $(document).mousedown(function() {
        alert('test');
    });
});

The problem is that you are using the global window.event object, and not jQuery's event object. window.event only works in some browsers, and it is not a W3C standard.

jQuery normalizes the event object so it's the same in all browsers. The event handler is passed that jQuery event object as a parameter. You should be using that.

 $(".class_name").mousedown(function (e) {

  switch (e.which) {
    case 1: //leftclick
        //...
        break;
    case 3: //rightclick
        //...
        break;
  }
});
发布评论

评论列表(0)

  1. 暂无评论