简体   繁体   English

javascript onclick触发键点击(使用jQuery)

[英]javascript onclick trigger key click (using jQuery)

How can I activate a "left" or "right" keyboard arrow push on click of a div. 如何在点击div时激活“左”或“右”键盘箭头。

So for example 所以举个例子

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

(I know that 37 is left) (我知道剩下37

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. 您当然可以将document.body替换为绑定了keypresskeydown事件的任何其他节点。

Reference: .trigger() 参考: .trigger()

From Definitive way to trigger keypress events with jQuery : 确定的方式使用jQuery触发keypress事件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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