使用html按钮在键盘上按下退出键

时间:2014-03-29 06:22:48

标签: javascript jquery html button

HTML按钮,按下时模拟按下ESC kayboard键。 因此,单击该按钮与用户在键盘上按ESC键具有相同的效果

如果无论如何肯定不可能请告诉我。

任何方法都可以。

编辑:我不想按ESC键来触发某些东西,我想要反向,触发ESC键的东西

2 个答案:

答案 0 :(得分:2)

试用此代码

<强> JS

$('body').keydown(function(e){
    if (e.which==27){
        alert("hh");
    }

});

$('#trigger').click(function(){
    var e=$.Event('keydown');
    e.which=27;
    $('body').trigger(e);
});

<强> HTML

<input type="button" id="trigger" value="trigger button" />

<强> DEMO HERE

答案 1 :(得分:0)

使用JQuery,您可以创建Key Press Event并将该事件与Button Click绑定。像:

<input type="button" id="esc" value="Esc like button">


<script>
$('body').keydown(function(e) {
    if (e.keyCode == 27) {
        // put your code here if any (that will run after Esc pressed).
        alert('Esc Pressed');
    }    
});

var e = $.Event("keydown", {
    keyCode: 27
});

$('#esc').click(function() {
    $("body").trigger(e);
});
</script>

Fiddle DEMO