VUEJS项目实践四之自定义键盘指令(按键即获取焦点)
发布日期:2021-05-10 11:42:29 浏览次数:16 分类:精选文章

本文共 2597 字,大约阅读时间需要 8 分钟。

���������������������������������������������������������������������

��� Vue.js ������������������������������������������������������������������������������������������ DOM ������������������������������������������������������������������������������

��������������������������� Vue ������

��������������������� Vue ��������������������������������������������������������������������������������� keyDirective.js���

���������������

import Vue from 'vue'
Vue.directive('keyBindListen', {
bind: function(el, binding) {
document.onkeydown = function(e) {
var ev = e ? e : window.event
var curkey = ev.keyCode || ev.which
curkey = keyMapping(curkey)
if (curkey) {
var key = keyMapping(curkey)
// ������������ bind_key ���������������������
$('[bind_key]').each(function() {
if (key === $(this).attr('bind_key')) {
// ��������������������� DOM ������
var id = $(this).attr('id')
$('#' + id).focus().click()
}
})
}
}
}
})
// ��������������� keyMapping ������������������������������������������
function keyMapping(keycode) {
var key = ''
switch (keycode) {
case 27:
key = 'ESC'
break;
case 33:
key = 'PAGEUP'
break;
case 34:
key = 'PAGEDOWN'
break;
// ���������������������������������������������
default:
key = String.fromCharCode(keycode)
}
return key
}

��������������������������������������������� ��� Vue ���������������������������������������������������������������������������������������������������

��������������������������������� ��� keyMapping ���������������������������������������������������������������������������������������

function keyMapping(keycode) {
let key = ''
switch (keycode) {
case 27:
key = 'ESC'
break;
case 33:
key = 'PAGEUP'
break;
// ������������������������
default:
key = String.fromCharCode(keycode)
}
return key
}

���������������������

  • bind_key ��������������������������������������� bind_key ���������������������������

  • keyMapping ���������������������������������������������������������������������������������������������������������������

������������������������������������������������ v-key-bind-listen ������������������������������������ bind_key ��������������������������������������������� DOM ������������������

������������������������������������������������������������������������������������������������������������������������

上一篇:VUEJS项目实践五之Dialog弹出框MessageBox(超好看的bootstrap样式)
下一篇:VUEJS项目实践三之引入bootstrap

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年03月31日 08时23分54秒