jQuery event.which不工作在IE [英] jQuery event.which not working in IE

查看:111
本文介绍了jQuery event.which不工作在IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery函数来获取事件的代码。它在FF中工作得很好,但在IE8中,我只是得到未定义。有没有一些原因,这在IE8中不起作用?我正在使用jQuery版本1.3.2,代码在下面(简化)。

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 code of the pressed button
   keycode = event.which;
}

谢谢,
大都会

Thanks, Metropolis

** EDITED **

** EDITED **

基本上我想要做的是拥有一个带有onkeyup事件属性的HTML元素,它将调用handleInput函数。这是我想要的HTML的样子。

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)" />

有没有办法传递这样的事件,并让jQuery识别?

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

推荐答案

jQuery支持 event.which 。你如何绑定你的处理程序?你在使用jQuery吗?如果你在做:

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

});

它应该工作。您可能不想使用事件(请注意,我传递 evt 事件)作为参数,因为它将与IE的事件对象。我相信这是您问题的根源。

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.

编辑

阅读后您的评论,您需要在< HEAD> 中引用的Javascript文件中执行此操作:

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
});

这篇关于jQuery event.which不工作在IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆