将 tabindex 限制为页面的一部分
情况:
我有一个网页,它打开模式窗口(灯箱),其中包含用户可以输入数据的表单。用户通常使用键盘进行导航,从一个字段切换到下一个字段。
问题:
当模态窗口打开时,只有窗口处于活动状态,无法使用鼠标访问页面的其余部分,但可以通过按 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
捕获
tab 键
怎么样?在最后一个元素上,然后将焦点放在第一个元素上,反之亦然,使用shift-tab
这是我在多模块对话环境中使用的,以将焦点保持在对话框中,在使用鼠标或其他键
小编辑的对话框:通过 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 withshift-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
small edit: replaced my on "unibind()" (=.off(x).on(x)) function through jQuery "on()"
如果您想将焦点限制在
所有现代浏览器支持的dom“parent”内
in case you want to restrict the focus inside dom "parent"
supported by all mordern browsers
查看 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.
何时在 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.
尽管这是一篇旧帖子,但我一直在寻找该问题的解决方案,并执行了以下操作来解决它。
使用 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.