在 javascript 或 JQuery 中触发或返回 shift+tab

发布于 2025-01-06 01:58:33 字数 523 浏览 0 评论 0原文

我为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌辣 2025-01-13 01:58:33

在这种情况下,触发器仅调用主体上定义的事件处理程序。它不会将键代码发送到浏览器。请参阅:http://api.jquery.com/trigger/。简而言之,它只执行 jQuery 知道的在发生 keydown 事件时执行的代码。它“模拟”按键事件。

据我所知,向浏览器发送关键事件是不可能的,因为这会带来一些严重的安全风险。您必须在 Javascript 中实现选择下一个和上一个元素。

编辑:
您可以尝试类似的方法:

$(function() {
  var navigationElements = $('[tabIndex]').sort(function(a,b) {
    return $(a).attr('tabindex') - $(b).attr('tabindex');
  });

  // We don't know if the users presses the up or down button first
  // so we initialize as null and let the event handle the first element
  var currentIndex = null;

  // On event:
  if (currentIndex != null && currentIndex < navigationElements.length - 1) {
    navigationElements[++currentIndex].focus();
  } else {
    currentIndex = 0;
    navigationElements[currentIndex].focus();
  }

  // and:
  if (currentIndex != null && currentIndex > 0) {
    navigationElements[--currentIndex].focus();
  } else {
    currentIndex = navigationElements.length - 1;
    navigationElements[currentIndex].focus();
  }
});

这是未经测试、未优化的代码,因此可能有更好的解决方案,但即使在速度较慢的浏览器上,它也会运行得相当快。它不考虑不经过您的事件的焦点更改,因此您必须添加 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:

$(function() {
  var navigationElements = $('[tabIndex]').sort(function(a,b) {
    return $(a).attr('tabindex') - $(b).attr('tabindex');
  });

  // We don't know if the users presses the up or down button first
  // so we initialize as null and let the event handle the first element
  var currentIndex = null;

  // On event:
  if (currentIndex != null && currentIndex < navigationElements.length - 1) {
    navigationElements[++currentIndex].focus();
  } else {
    currentIndex = 0;
    navigationElements[currentIndex].focus();
  }

  // and:
  if (currentIndex != null && currentIndex > 0) {
    navigationElements[--currentIndex].focus();
  } else {
    currentIndex = navigationElements.length - 1;
    navigationElements[currentIndex].focus();
  }
});

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 the currentIndex to the element that was lastly selected by the user.

ㄟ。诗瑗 2025-01-13 01:58:33
const keyupEvent = new Event('KeyboardEvent');
keyupEvent.initEvent('keyup');
Object.defineProperties(keyupEvent, {
  keyCode: {
    value: 9
  },
  shiftKey: {
    value: true
  }
});

...

element.dispatch(keyupEvent);

const keyupEvent = new Event('KeyboardEvent');
keyupEvent.initEvent('keyup');
Object.defineProperties(keyupEvent, {
  keyCode: {
    value: 9
  },
  shiftKey: {
    value: true
  }
});

...

element.dispatch(keyupEvent);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文