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
2 Answers
Reset to default 5use 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;
}
});