我有一個問題,我的代碼如下問題:KEYDOWN不註冊
$(document).ready(function() {
var currenty = 0;
var currentx = 0;
$("body").keypress(function(event) {
if (event.keyCode === 68) {
$(".box").animate({marginLeft: currenty + 30}, 200);
currenty += 30;
} else if (event.keyCode === 65) {
$(".box").animate({marginLeft: currenty - 30}, 200);
currenty -= 30;
} else if (event.keyCode === 87) {
$(".box").animate({marginTop: currenty + 30}, 200);
currentx += 30;
} else if (event.keyCode === 83) {
$(".box").animate({marginTop: currenty - 30}, 200);
currentx -= 30;
} else {
}
});
});
我這段代碼的問題是,當我按一個鍵,沒有任何反應。
編輯:看來,如果我第一個鍵碼改爲32(空格鍵)工作
再次編輯:只是解決我的問題,該鍵碼的地方爲大寫字母
瀏覽器控制檯中顯示哪些錯誤? –
只有event.returnValue已被棄用。請改用標準的event.preventDefault()。 – spazhead
我認爲這裏的工作http://jsfiddle.net/VZ5c6/ –