2012-08-09 48 views
0

如何通過javascript在網頁上模擬ESC按鍵?如何通過javascript模擬網頁上的ESC按鍵?

非常感謝您的幫助!

+0

可能重複[模擬左,用JavaScript右箭頭鍵事件](http://stackoverflow.com/questions/ 5516600/simulate-left-and-right-arrow-key-event-with-javascript) – 2012-08-09 12:10:11

+0

這個答案是[使用initKeyboardEvent](http://stackoverflow.com/questions/11857524/working-with-initkeyboardevent/11857825# 11857825)可能會有所幫助。 – RobG 2012-08-09 12:20:35

回答

-1

沒有jQuery的:

document.onkeydown = checkKeycode; 
function checkKeycode(e) { 
var keycode; 
    if (window.event) { 
    keycode = window.event.keyCode; 
    } 
    else if (e) { 
    keycode = e.which; 
    } 
    // enter 
    if (keycode == 13) { 

    } 

    // escape 
    if (keycode == 27) { 
    //place your action here 
    } 
} 
+0

但我需要通過javascript代碼(不是由用戶)按ESC。 – Dmitry 2012-08-09 12:08:19

+0

這是檢測何時按下Esc鍵,它不會模擬它。 – 2012-08-09 12:10:02

-1

你的意思是這樣

$(document).delegate('input','keypress', function(e){ 
    code = e.keyCode ? e.keyCode : e.which; 
    if(code=== 27){ 
     alert('esc pressed'); 
    } 
}); 
+0

這是檢測何時按下esc鍵,它不會模擬它。 – 2012-08-09 12:08:19