将 tabindex 限制为页面的一部分

发布于 2024-10-21 00:28:01 字数 415 浏览 1 评论 0原文

情况:

我有一个网页,它打开模式窗口(灯箱),其中包含用户可以输入数据的表单。用户通常使用键盘进行导航,从一个字段切换到下一个字段。

问题

当模态窗口打开时,只有窗口处于活动状态,无法使用鼠标访问页面的其余部分,但可以通过按 Tab 键移出模态窗口来访问元素。

问题:

如何通过使用选项卡按钮将移动限制为仅表单窗口内的元素?

我唯一能想到的是,当模式窗口打开时,使用 Javascript 在所有表单元素(和其他可聚焦元素)上设置 tabindex=-1 ,然后设置 tabindex > 当模态窗口关闭时,值恢复为之前的值。 有更简单/更好的方法吗?

Situation:

I have a webpage which opens modal windows (light boxes) which contain forms where the user can input data. Users generally navigate using the keyboard, tabbing from one field to the next.

Problem:

When a modal window opens, only the window is active, the rest of the page is not accessible using the mouse, but elements can be reached by tabbing out of the modal window.

Question:

How can I restrict movement by using the tab button to only the elements within the form window?

The only thing I can think of is using Javascript to set tabindex=-1 on all form elements (and other focusable elements) when the modal window is opened and then set the tabindex values back to their previous values when the modal window is closed.
Is there a simpler/better way?

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

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

发布评论

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

评论(5

南汐寒笙箫 2024-10-28 00:28:01

捕获 tab 键 怎么样?在最后一个元素上,然后将焦点放在第一个元素上,反之亦然,使用 shift-tab

这是我在多模块对话环境中使用的,以将焦点保持在对话框中,在使用鼠标或其他键

inputs=".editing, input, textarea, button, a, select"
no_tab="[type='hidden'], :disabled"

$focusable=dlg.$form.find(inputs).not(no_tab)


$fa_first=$focusable.first()
$fa_last=$focusable.last()

$fa_last.on("keydown", (evt) =>
    if evt.keyCode==9 && ! evt.shiftKey
        $fa_first.focus()
        evt.preventDefault()
        false
)
$fa_first.on("keydown", (evt) =>
    if evt.keyCode==9 && evt.shiftKey
        $fa_last.focus()
        evt.preventDefault()
        false
)

小编辑的对话框:通过 jQuery“on()”替换我的 on“unibind()”(=.off(x).on(x)) 函数

How about catching the tab-key? On the last element and then put the focus on the first and vice versa with shift-tab

This I am using in a multi-modless-diaolog environment, to keep the focus with in a Dialog, switching between dialogs with mouse or other key

inputs=".editing, input, textarea, button, a, select"
no_tab="[type='hidden'], :disabled"

$focusable=dlg.$form.find(inputs).not(no_tab)


$fa_first=$focusable.first()
$fa_last=$focusable.last()

$fa_last.on("keydown", (evt) =>
    if evt.keyCode==9 && ! evt.shiftKey
        $fa_first.focus()
        evt.preventDefault()
        false
)
$fa_first.on("keydown", (evt) =>
    if evt.keyCode==9 && evt.shiftKey
        $fa_last.focus()
        evt.preventDefault()
        false
)

small edit: replaced my on "unibind()" (=.off(x).on(x)) function through jQuery "on()"

懷念過去 2024-10-28 00:28:01

如果您想将焦点限制在

parent.addEventListener('focusout', function(event) {
    event.stopPropagation();

    if (node.contains(event.relatedTarget)) {  // if focus moved to another 
                                                              parent descend
        return;
    }

    parent.focus();  // otherwise focus on parent or change to another dom
})

所有现代浏览器支持的dom“parent”内

in case you want to restrict the focus inside dom "parent"

parent.addEventListener('focusout', function(event) {
    event.stopPropagation();

    if (node.contains(event.relatedTarget)) {  // if focus moved to another 
                                                              parent descend
        return;
    }

    parent.focus();  // otherwise focus on parent or change to another dom
})

supported by all mordern browsers

思念满溢 2024-10-28 00:28:01

查看 jQuery BlockUI 插件。他们有使用带有两个按钮的模式框的示例,并且它也限制选项卡。

它可能会或可能不会与您的模态窗口一起开箱即用,但值得一看,而不必实现您自己的解决方法。

Have a look at the jQuery BlockUI Plugin. They have an example using a modal box with two buttons, and it restricts tabbing as well.

It may or may not work out-of-the-box with your modal windows, but it's worth a look instead of having to implement your own work-around.

淡写薰衣草的香 2024-10-28 00:28:01

何时在 React 中执行此操作。我可以像您在问题中所说的那样传递 tabIndex 。我还必须尝试使用​​pointerEvents none css,对于我的情况下的不可见部分。

When do this in React . I can pass tabIndex like you said in question . And I also have to try with pointerEvents none css , for my invisible section in my case.

等风也等你 2024-10-28 00:28:01

尽管这是一篇旧帖子,但我一直在寻找该问题的解决方案,并执行了以下操作来解决它。

使用 JQuery,一旦模态窗口打开,我就禁用了不同表单和 div 中的所有输入字段(模态表单本身的输入字段除外)。

$('#formId :input').prop('disabled',true);

当模态表单关闭时,您可以再次启用输入元素。

在页面周围“切换”时不会考虑禁用字段。

Even though it is an old post I was looking for a solution to this problem and did the following to solve it.

Using JQuery I disabled all input fields in different forms and divs as soon as the modal window opens up (except the ones on the modal form itself).

$('#formId :input').prop('disabled',true);

when the modal form is closed, you can enable the input elements again.

Disabled fields are not considered when "tabbing" around your page.

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