如何使用纯JS或jQuery检测转义键?

本文介绍如何使用纯JavaScript或jQuery检测转义键。包括在不同浏览器(IE、Firefox、Chrome)中检测转义键的方法,以及如何使用keyCode和key属性进行检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:How to detect escape key press with pure JS or jQuery?

Possible Duplicate: 可能重复:
Which keycode for escape key with jQuery 使用jQuery的escape key的哪个键代码

How to detect escape key press in IE, Firefox and Chrome? 如何在IE,Firefox和Chrome中检测转义按键? Below code works in IE and alerts 27 , but in Firefox it alerts 0 下面的代码适用于IE和警报27 ,但在Firefox中它会提醒0

$('body').keypress(function(e){
    alert(e.which);
    if(e.which == 27){
        // Close my modal window
    }
});

#1楼

参考:https://stackoom.com/question/E8aH/如何使用纯JS或jQuery检测转义键


#2楼

Using JavaScript you can do check working jsfiddle 使用JavaScript,您可以检查工作jsfiddle

document.onkeydown = function(evt) {
    evt = evt || window.event;
    if (evt.keyCode == 27) {
        alert('Esc key pressed.');
    }
};

Using jQuery you can do check working jsfiddle 使用jQuery你可以检查工作jsfiddle

jQuery(document).on('keyup',function(evt) {
    if (evt.keyCode == 27) {
       alert('Esc key pressed.');
    }
});

#3楼

Best way is to make function for this 最好的办法就是为此做好准备

FUNCTION: 功能:

$.fn.escape = function (callback) {
    return this.each(function () {
        $(document).on("keydown", this, function (e) {
            var keycode = ((typeof e.keyCode !='undefined' && e.keyCode) ? e.keyCode : e.which);
            if (keycode === 27) {
                callback.call(this, e);
            };
        });
    });
};

EXAMPLE: 例:

$("#my-div").escape(function () {
    alert('Escape!');
})

#4楼

check for keyCode && which & keyup || 检查keyCode && whichkeyup || keydown

$(document).keydown(function(e){
   var code = e.keyCode || e.which;
   alert(code);
});

#5楼

Note: keyCode is getting deprecated use key instead. 注意: keyCode正在被弃用的使用key

function keyPress (e) {
    if(e.key === "Escape") {
        // write your logic here.
    }
}

Here is jsfiddle 这是jsfiddle


keyCode is Getting deprecated keyCode已弃用

It seems keydown and keyup work, even though keypress may not 它似乎是keydownkeyup工作,即使keypress可能没有


$(document).keyup(function(e) {
     if (e.key === "Escape") { // escape key maps to keycode `27`
        // <DO YOUR WORK HERE>
    }
});

Which keycode for escape key with jQuery 使用jQuery的escape key的哪个键代码


#6楼

The keydown event will work fine for Escape and has the benefit of allowing you to use keyCode in all browsers. keydown事件适用于Escape,并且允许您在所有浏览器中使用keyCode Also, you need to attach the listener to document rather than the body. 此外,您需要将侦听器附加到document而不是正文。

Update May 2016 2016年5月更新

keyCode is now in the process of being deprecated and most modern browsers offer the key property now, although you'll still need a fallback for decent browser support for now (at time of writing the current releases of Chrome and Safari don't support it). keyCode现在正在被弃用,现在大多数现代浏览器都提供了key属性,尽管你现在仍然需要支持适当的浏览器支持(在编写当前版本的Chrome和Safari时不支持它) )。

Update September 2018 evt.key is now supported by all modern browsers. 2018年9月更新 evt.key现在支持所有现代浏览器。

 document.onkeydown = function(evt) { evt = evt || window.event; var isEscape = false; if ("key" in evt) { isEscape = (evt.key === "Escape" || evt.key === "Esc"); } else { isEscape = (evt.keyCode === 27); } if (isEscape) { alert("Escape"); } }; 
 Click me then press the Escape key 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值