事件没有定义在mozilla firefox的javascript函数? [英] event is not defined in mozilla firefox for javascript function?

查看:108
本文介绍了事件没有定义在mozilla firefox的javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function onlyNumeric() {   
    if (event.keyCode < 48 || event.keyCode > 57) {
        event.returnValue = false; 
    }

}

onkeypress=onlyNumneric();

在IE中,此代码运行正常。

In IE, this code is working fine. However, in Mozilla Firefox, the event is an undefined error.

推荐答案

在FF / Mozilla中,事件被传递给事件处理程序作为参数。使用类似下面的东西来解决IE中缺少的事件参数。

In FF/Mozilla the event is passed to your event handler as a parameter. Use something like the following to get around the missing event argument in IE.

 function onlyNumeric(e)
 {
     if (!e) {
        e = window.event;
     }

     ...
 }

你会发现,两者之间还有一些其他的差异。此链接有一些有关如何检测跨浏览器中按下哪个键的信息方式。

You'll find that there are some other differences between the two as well. This link has some information on how to detect which key is pressed in a cross-browser way.

这篇关于事件没有定义在mozilla firefox的javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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