简体   繁体   中英

javascript onclick trigger key click (using jQuery)

How can I activate a "left" or "right" keyboard arrow push on click of a div.

So for example

$('.item').click(function(){
    keyCode(37);
});

(I know that 37 is left)

You would go like

$('.item').click(function(){
    $( document.body ).trigger({
        type: 'keypress',
        which: 37,
        keyCode: 37
    });
});

You can of course replace document.body with any other node that has a keypress or keydown event bound to it.

Reference: .trigger()

From Definitive way to trigger keypress events with jQuery :

var e = jQuery.Event("keypress");
e.which = 37; // # Some key code value
$("div").trigger(e);

not sure, but can you try this.

$('.item').click(function(){ 
      $('body').trigger("keypress", [{ 
        preventDefault:function(){}, 
        keyCode:13 
     }]); 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM