keyup绑定在Firefox中不起作用

时间:2022-12-06 19:26:21

You can see my project here -

你可以在这里看到我的项目 -

http://www.inluxphoto.com/custom/jsgallery/index.php

I am attempting to make the left and right arrow keys move the slideshow. I was able to get it to work in all browsers by following exactly the instructions on the front page of jqueryfordesigners.com (sorry I am only allowed one link).

我试图让左右箭头键移动幻灯片。我能够完全遵循jqueryfordesigners.com首页上的说明,让它在所有浏览器中运行(对不起,我只允许一个链接)。

However, it is necessary that the keyup be unbinded until the animation completes, so the user cannot do a quick double tap of the key, which breaks the show. This led me to the following function -

但是,在动画完成之前,必须将键盘解除绑定,因此用户无法快速双击键,这会破坏节目。这让我有了以下功能 -

function keyCommands() {

  //Bind Keys according to keyAssignments
  function keyCommandBind() {
   $(document.documentElement).bind('keyup', keyAssignments)   
  }

  //Bind functions to specific keys
  function keyAssignments() {

   if (event.keyCode == 37) {
    leftArrow();
   }

   if (event.keyCode == 39) {
    rightArrow();
   }

   if (event.keyCode == 32) {
    spaceBar();
   }

  }

  function leftArrow() {
         //unbind, do stuff, rebind
  }

  function rightArrow() {
          //unbind, do stuff, rebind
  }

  function spaceBar() {
          //unbind, do stuff, rebind
  }

  keyCommandBind();

 }

This works in all browsers except Firefox & Camino. Firebug tells me event (ie event.keyCode) is not defined. That's true, it's not defined, and I understand that. However I don't understand why, if it's not defined, does it work in all other browsers.

这适用于除Firefox和Camino之外的所有浏览器。 Firebug告诉我事件(即event.keyCode)没有定义。这是真的,它没有定义,我理解。但是我不明白为什么,如果它没有定义,它是否适用于所有其他浏览器。

How can I appropriately define this? Or, am I doing it wrong?

我该如何恰当地定义这个?或者,我做错了吗?

Any help would be most appreciated, thanks for your time!

任何帮助都将非常感谢,感谢您的时间!

2 个解决方案

#1


2  

Try declaring keyAssignments as function keyAssignments(event) {. I'm not sure why it would work in some but not others, maybe they have a reserved variable that bubbles with the keyup event by default?

尝试将keyAssignments声明为function keyAssignments(event){。我不确定为什么它会在某些部分而不是其他部分工作,也许它们有一个保留变量,默认情况下会与keyup事件一起冒泡?

#2


0  

So when we do keypress listeners here, this is some code we use:

所以当我们在这里做按键监听器时,这是我们使用的一些代码:

$('.selector').keypress(function(e) {
var key = e.which || e.keyCode || e.keyChar;
    //etc.
  )};

Basically some browsers call it different things.

基本上有些浏览器称它为不同的东西。

#1


2  

Try declaring keyAssignments as function keyAssignments(event) {. I'm not sure why it would work in some but not others, maybe they have a reserved variable that bubbles with the keyup event by default?

尝试将keyAssignments声明为function keyAssignments(event){。我不确定为什么它会在某些部分而不是其他部分工作,也许它们有一个保留变量,默认情况下会与keyup事件一起冒泡?

#2


0  

So when we do keypress listeners here, this is some code we use:

所以当我们在这里做按键监听器时,这是我们使用的一些代码:

$('.selector').keypress(function(e) {
var key = e.which || e.keyCode || e.keyChar;
    //etc.
  )};

Basically some browsers call it different things.

基本上有些浏览器称它为不同的东西。