[問題] 請教一下jQuery的keydown事件

看板Ajax作者 (小Ya)時間14年前 (2011/02/18 21:42), 編輯推噓3(309)
留言12則, 4人參與, 最新討論串1/1
嗯…是這樣的,最近在練習jQuery 想試著寫魔術方塊的遊戲,雖然不知道方法對不對 不過才剛寫就遇到問題了 在按下方向鍵左和右的時候會出問題,但是按方向鍵下是正常的 $("html").keydown(function(e){ if(e.which == 40){ moveDown(); }else if(e.which == 37){ moveLeft(); }else if(e.which == 39){ moveRight(); } }); function moveDown(){ $(".control").each(function(){ $(this).css("top", $(this).position().top + 20); }); } function moveLeft(){ $(".control").each(function(){ $(this).css("left", $(this).position().left - 20); }); } function moveRight(){ $(".control").each(function(){ $(this).css("left", $(this).position().left + 20); }); } 不知道這樣的Script有沒有什麼問題 看到錯誤碼寫著:charCode屬性不該用於keydown事件,此值無意義。 但是按方向鍵下是正常的,左右才會出問題 有人可以幫我解惑嗎?謝謝。 -- ╭──╮╭╮ ╰┐┌╯││ 低調才是王道 ││er│╰╮in~☆ ╰╯ ╰─╯ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.86.164.13

02/19 18:37, , 1F
e.which 改用 e.keyCode
02/19 18:37, 1F

02/19 18:39, , 2F
$("html") 改用 $(document) 試試
02/19 18:39, 2F

02/19 20:23, , 3F
如果改document就不能動了,改e.keyCode問題還是一樣
02/19 20:23, 3F

02/19 20:23, , 4F
而且還出現新錯誤,Attribute selector中有未預期的
02/19 20:23, 4F

02/19 20:24, , 5F
token:「!」
02/19 20:24, 5F

02/19 22:16, , 6F
http://jsfiddle.net/6U6sC/ 沒有問題..應該是 move裡有錯
02/19 22:16, 6F

02/19 23:20, , 7F

02/19 23:20, , 8F
左右就出問題了,還有…真是好網站~XD
02/19 23:20, 8F

02/20 12:58, , 9F
不是按鍵的問題 是位置設定寫得很奇怪
02/20 12:58, 9F

02/20 12:58, , 10F
不要用float, postion改為absolute 就可以了
02/20 12:58, 10F

02/21 01:02, , 11F
真的可以了耶,感謝K大~^^
02/21 01:02, 11F

02/21 06:10, , 12F
呦~ 阿德好久不見
02/21 06:10, 12F
文章代碼(AID): #1DNdSgxH (Ajax)
文章代碼(AID): #1DNdSgxH (Ajax)