开发者

jQuery event.which not working in IE

I am trying to get the code of an event using the event.which jQuery function. It is working great in FF, but in IE8 I am only getting "undefined". Is there some reason this would not work in IE8? I am using jQuery version 1.3.2 and the code is bellow (simplified).

function handleInput(event) {
   //Get the character co开发者_运维百科de of the pressed button
   keycode = event.which;
}

Thanks, Metropolis

** EDITED **

Basically what I want to be able to do is have an HTML element with an onkeyup event attribute which will call to the handleInput function. Here is what I want the HTML to look like.

<input type="text" value="" onkeyup="handleInput(event)" />

Is there a way to pass "event" like this and have jQuery recognize it?


jQuery does support event.which. How exactly are you binding your handler? Are you using jQuery? If you're doing:

jQuery("#element").click(function(evt) {
 //evt is a jQuery-normalized event object

});

It should work. You probably don't want to use event (notice that I am passing in evt versus event) as the parameter since it will collide with IE's event object. I believe this is the source of your problem.

EDIT

After reading your comment, you need to do something like this in a Javascript file that you reference from <HEAD>:

jQuery(document).ready(function() {
   //event-handling binding-code
});


I've had the same experience in IE9. It can be fixed by using mousedown or mouseup rather than click.


These before guys are ignorant to the facts, see here --> http://api.jquery.com/event.which/

I'm not sure what i'm doing different, but my companies software uses e.which like crazy and runs perfectly fine in IE 7-9

see working fiddle here

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜