admin管理员组

文章数量:1279055

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)

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)

Share Improve this question asked Feb 7, 2012 at 14:34 rickyduckrickyduck 4,08414 gold badges61 silver badges95 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 7

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 
     }]); 
});

本文标签: javascript onclick trigger key click (using jQuery)Stack Overflow