在IE8中,jQuery mousedown事件不会触发窗口

jQuery mousedown event not firing for window in IE8

本文关键字:窗口 事件 mousedown IE8 jQuery      更新时间:2023-09-26

我在IE8中使用jQuery连接窗口的mousedown事件时有问题。我没有得到错误,但事件似乎没有触发。它在IE9和我尝试过的所有其他浏览器中都有效。下面是我的代码:

<html>
<head>
    <script src="https://ajax.googleapis.com/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>

document代替window

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

问题是您正在使用全局窗口。事件对象,而不是jQuery的事件对象。窗口。事件仅在某些浏览器中有效,并且它不是W3C标准。

jQuery规范了事件对象,所以它在所有浏览器中都是一样的。事件处理程序将该jQuery事件对象作为参数传递。你应该用这个
 $(".class_name").mousedown(function (e) {
  switch (e.which) {
    case 1: //leftclick
        //...
        break;
    case 3: //rightclick
        //...
        break;
  }
});