我有两个功能。当按下回车时,功能正常运行,但当按下转义时,它不会。转义键的正确数字是多少?
$(document).keypress(function(e) { if (e.which == 13) $('.save').click(); // enter (works as expected) if (e.which == 27) $('.cancel').click(); // esc (does not work) });
尝试使用keyup 事件:
$(document).on('keyup', function(e) { if (e.key == "Enter") $('.save').click(); if (e.key == "Escape") $('.cancel').click(); });