在 javascript 或 JQuery 中触发或返回 shift+tab
我为 Windows CE 制作了一个页面,使用 tabIndex 进行二维导航。起初我只是想用tabIndex作为下一个焦点的参考。
因为此应用程序适用于便携式设备,所以与简单的 Tab 和 Shift+Tab 相比,这样做对手持设备的要求非常高,并且非常耗时。
我有一个函数来处理向上箭头和向下箭头的 onkeydown, 对于我的向下箭头,代码很简单
if(specialMove == 0){
所做的只是检查选项卡是否离开页面。
event.keyCode=9; return;}
问题是我的向上箭头,我希望它返回 SHIFT+TAB 来向上导航,我找到了这个 jQuery 代码片段,但我在使其工作时遇到问题。
if(specialMove == 0){
$('body').trigger(JQuery.Event('keydown', {keyCode:9, shiftKey: true}));
return;
}
I made a page for a windows CE with a two dimentional navigation using tabIndex. at first I just wanted to use tabIndex as a reference for the next focus.
because this application is for a portable device, doing it like this is very demanding on the handheld and very time consuming when comparing to a simple tab and shift+tab.
I have a function to process the onkeydown for my up arrow and down arrow,
for my downarrow, the code is simple
if(specialMove == 0){
all this does is checks that the tab isn't leaving the page.
event.keyCode=9; return;}
the problem is my uparrow, I want it to return a SHIFT+TAB to navigate upwards, I found this snipet of jQuery code but I'm having a problem making it work.
if(specialMove == 0){
$('body').trigger(JQuery.Event('keydown', {keyCode:9, shiftKey: true}));
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,触发器仅调用主体上定义的事件处理程序。它不会将键代码发送到浏览器。请参阅:http://api.jquery.com/trigger/。简而言之,它只执行 jQuery 知道的在发生 keydown 事件时执行的代码。它“模拟”按键事件。
据我所知,向浏览器发送关键事件是不可能的,因为这会带来一些严重的安全风险。您必须在 Javascript 中实现选择下一个和上一个元素。
编辑:
您可以尝试类似的方法:
这是未经测试、未优化的代码,因此可能有更好的解决方案,但即使在速度较慢的浏览器上,它也会运行得相当快。它不考虑不经过您的事件的焦点更改,因此您必须添加
onfocus
事件以将currentIndex
设置为最后选择的元素由用户。Trigger only calls the event handlers defined on the body in this case. It does not send the key codes to the browser. See: http://api.jquery.com/trigger/. In short, it only executes the code that jQuery knows of that would be executed in case of a keydown event. It 'simulates' a keydown event.
Sending key events to the browser is not possible as far as I know, as this poses some serious security risks. You'll have to implement selecting the next and previous element in Javascript.
EDIT:
You could try something like:
This is untested, unoptimized code, so there probably is a much better solution but this would work pretty fast even on slow browsers. It does not take into account focus changes that do not go through your events, so you'd have to add
onfocus
events to set thecurrentIndex
to the element that was lastly selected by the user.