按键事件不能在IE和Chrome中工作,但在FF中工作 [英] keypress event not working in IE and Chrome but working in FF

查看:154
本文介绍了按键事件不能在IE和Chrome中工作,但在FF中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法为什么会发生这种情况?我通常会认为Chrome会更加宽容这些代码?

$ $ $(document).keypress(function(e){ ();
if(e.keyCode == 37)leftImage();
});

这就是我的按键的样子。我错过了什么吗?右图();和leftImage();是功能,应该工作,因为我在其他地方使用这些功能



感谢您的帮助!

解决方案

更改按键 keydown

  $(document).keydown(function(e){
if(e.keyCode == 39)rightImage();
if (e.keyCode == 37)leftImage();
});

请参阅 http://www.bloggingdeveloper.com/post/KeyPress-KeyDown-KeyUp-The-Difference-Between-Javascript-Key -Events.aspx 两者之间的解释。


Any idea why this might happen? I would usually think that Chrome would be more forgiving with the codes?

$(document).keypress(function(e) {
    if(e.keyCode == 39) rightImage();
    if(e.keyCode == 37) leftImage();
});

Thats what my keypress key looks like. Am I missing something? rightImage(); and leftImage(); are functions which should be working becase I'm using those functions somewhere else too

Thanks for the help!

解决方案

Change keypress to keydown:

$(document).keydown(function(e) {
    if(e.keyCode == 39) rightImage();
    if(e.keyCode == 37) leftImage();
});

See http://www.bloggingdeveloper.com/post/KeyPress-KeyDown-KeyUp-The-Difference-Between-Javascript-Key-Events.aspx for an explanation between the two.

这篇关于按键事件不能在IE和Chrome中工作,但在FF中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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