学习JS发送自定义键盘(KeyboardEvent)事件的过程当中,遇到了一个小难题:单个按键Tab能够正常发送,焦点可以转移到下一个元素,但想实现Shift+Tab,反向移动焦点时,却被DOM3的浏览器兼容性问题难到了。html
void initKeyboardEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString keyIdentifierArg, in unsigned long keyLocationArg, in DOMString modifiersList);
根据资料1的经验,使用DOM3标准来调用,结果仍是失败。浏览器
void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view, const String &keyIdentifier, unsigned location, bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey) { if (dispatched()) return; initUIEvent(type, canBubble, cancelable, view, 0); m_keyIdentifier = keyIdentifier; m_location = location; m_ctrlKey = ctrlKey; m_shiftKey = shiftKey; m_altKey = altKey; m_metaKey = metaKey; m_altGraphKey = altGraphKey; }
按此原型再次调用,终于成功!(PS:开源就是好啊!)函数
模拟Shift+Tab代码以下:学习
var evt = document.createEvent("KeyboardEvent"); evt.initKeyboardEvent("keydown", true, false, window, 'U+0009',0,false,false,true,false,false); e.currentTarget.dispatchEvent(evt);
By:Asion Tang
AT:2013年9月20日 23:58:40spa