问题
$(document).ready(function(){
$(document).on( 'keypress', 'body', function( e ){
var code = e.keyCode || e.which;
if( code = 110 ) {
console.log( 'Successfully triggered ALT+N' );
}
});
$(document).on( 'click', '#copy', function( ){
var e = $.Event('keypress');
e.which = 110; // Character 'A'
console.log( e );
$('body').trigger(e);
});
});
The above piece of code successfully triggers the keyboard shortcut Alt+N on my document.
Alt+N keyboard shortcut does a particular action on my web page such as copying content of all form fields through Firefox plugin.
When #copy
button is clicked through mouse, the keyboard action triggered through jQuery but the corresponding action is not taking place.
But when i press Alt+N from keyboard directly the action occurs.
Is it possible to trigger a keyboard shortcut through mouse click event?
来源:https://stackoverflow.com/questions/22650342/trigger-a-keyboard-shortcut-through-mouse-click-event-using-jquery