使用左右箭头设置焦点(HTML + JS)

发布于 2024-07-29 23:14:40 字数 428 浏览 2 评论 0原文

是否有任何方法可以使左箭头的行为类似于选项卡按钮(将焦点设置为下一个可聚焦项目),而右箭头的行为类似于shift+tab(将焦点设置为上一个可聚焦项目)?

我已经走到这一步了:

$().keypress(function(e) {
    if (e.keyCode == 37) {
        alert('I want to do a shift-tab');
    }
    else if (e.keyCode == 39) {
        alert('I want to do a tab');
    }
});

但是谷歌并没有那么有帮助,所以我想我应该在谷歌上搜索更多内容的同时在这里发布一个快速帖子。

谢谢!

哦,而且它纯粹是针对 FF3.0 的,所以我不需要担心其他浏览器的麻烦。

Is there any to make the left arrow behave like the tab button (set focus to the next focusable item) and the right arrow behave like a shift+tab (set focus to the previous focusable item)?

I've gotten this far:

$().keypress(function(e) {
    if (e.keyCode == 37) {
        alert('I want to do a shift-tab');
    }
    else if (e.keyCode == 39) {
        alert('I want to do a tab');
    }
});

But google isn't being that helpful so I thought I'd put a quick post up here while I google some more.

Thanks!

Oh, and it's purely for FF3.0 so I don't need to worry about other browser nuisances.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

聽兲甴掵 2024-08-05 23:14:40

到目前为止你所拥有的看起来不错。

您可以保留“可聚焦”项目的列表,然后迭代该列表并保留指向当前项目的指针:

// or $('input')..depends what you want to focus on
var nodes = $('.focusable'), idx = -1;
$().keypress( function(e) {
   if (e.keyCode === 37) {
     nodes.get(++idx).focus();
    }
    else if (e.keyCode === 39) {
       nodes.get(--idx).focus();
    }
 });

但这只是一个粗略的轮廓。 在执行某些操作之前,您需要检查 idx,以确保不会越过可聚焦元素数组的开头或结尾。 当用户位于文本框内时,您还需要小心调用此方法。

What you have so far seems fine.

You could keep a list of 'focusable' items and just iterate through the list keeping a pointer to the current one:

// or $('input')..depends what you want to focus on
var nodes = $('.focusable'), idx = -1;
$().keypress( function(e) {
   if (e.keyCode === 37) {
     nodes.get(++idx).focus();
    }
    else if (e.keyCode === 39) {
       nodes.get(--idx).focus();
    }
 });

This is just a rough outline though. You'd need to check the idx before doing something to make sure you don't slip past the beginning or end of the focusable elements array. You'll also need to be careful about calling this when the user is inside of a textbox.

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