2011-01-28 66 views

回答

2

Is it possible to simulate key press events programmatically?

function simulateKeyPress(character) { 
    jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) }); 
} 

$(function() { 
    $('body').keypress(function(e) { 
    alert(e.which); 
    }); 

    simulateKeyPress("e"); 
}); 

DEMO

+0

很不錯的!它工作正常。我想知道是否有可能不觸發事件,但創建一個事件併發送到接受事件的函數。因此,事件根本不會發生。 – svirk 2011-01-28 21:01:09

相關問題