簡體   English   中英

使用箭頭鍵和jQuery移動項目

[英]Move item with arrow keys and jQuery

我有一個可以在形狀內拖動的項目。 我還能夠使用鍵盤上的箭頭鍵移動項目:

$(document).bind('keypress', function(event) {
  if(event.which === 63232){ // up arrow key
    if(!event.shiftKey) howMuch = -1;   
    else if(event.shiftKey) howMuch = -10;   
    moveText(howMuch);   
  }
});

但是,至少在FF中,這不再起作用了。 我警告了按下箭頭時發生的事件,所有四個都歸零。

如何檢測箭頭鍵按下? 哦,是的... javascript或jQuery。

謝謝您的時間,托德

您使用的鍵碼有誤,向上箭頭為38:

$(document).bind('keypress', function(event) {
    if (event.which === 38) {
        moveText(event.shiftKey ? -10 : -1);   
    }
});

箭頭鍵代碼供參考:

  • 左:37
  • 上:38
  • 右:39
  • 下:40

要查找其他鍵碼,請在API中查看keyPress()的示例

不只是:

$(document).on('keypress', function(e) { // Note I used .on()
    if (e.keyCode == 38) {                // I never use .which
        howMuch = (e.shiftKey) ? -10 : -1;
    }
    moveText(howMuch); // never seen this function before, isn't that it?
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM