功能
添加和删除键盘事件,支持几乎任何按键组合
引入
<script src="https://cdn.jsdelivr.net/gh/jeresig/jquery.hotkeys/jquery.hotkeys.js"></script>
用法
$(expression).bind(types, keys, handler);
$(expression).unbind(types, handler);
$(document).bind('keydown', 'ctrl+a', fn);
// e.g. replace '$' sign with 'EUR'
$('input.foo').bind('keyup', '$', function(){
this.value = this.value.replace('$', 'EUR');
});
使用 jQuery 的 on()/off 发光方法
$(expression).on(types, null, keys, handler);
$(expression).off(types, handler);
$(document).on('keydown', null, 'ctrl+a', fn);
// e.g. replace '$' sign with 'EUR'
$('input.foo').on('keyup', null, '$', function(){
this.value = this.value.replace('$', 'EUR');
});
支持的类型包括 keydown
,keyup
和 keypress
取消绑定
// won't work
$('#foo').unbind('keyup.$');
$('#foo').unbind('keyup.+');
//will work
$('#foo').unbind('keyup');
$('#foo').unbind('keyup.a');
$('#foo').unbind('keyup.ctrl_s');