控制弹出 javascript 小部件上下文中的选项卡焦点
我正在开发一个灯箱风格的 javascript 插件,它会弹出一个带有下一个+上一个按钮和关闭按钮的图像。我想做到这一点,以便选项卡只会在弹出窗口中显示的三个按钮之间跳转,而不是经过其中三个按钮,然后继续在后台的页面内容上。
有没有人对执行此操作的最佳方法有任何建议,目前我认为最好的方法是在弹出窗口出现时制作一组可选项卡元素,然后捕获选项卡以迭代该数组设置重点关注每个元素并防止默认选项卡行为。
有人知道这方面是否有任何最佳实践吗?
I'm working on a lightbox style javascript plugin that pops up an image with next+previous buttons and a close button. I want to make it so that tabbing will only jump between the three presented buttons in the popup, not go through the three of them and then continue on the page content in the background.
Does anyone have any suggestions on the best way to do this, currently I'm thinking that the best way is to make an array of tabbable elements when the popup appears and just capture tabs to iterate through that array setting focus on each one and preventing default tab behaviour.
Anyone know if there are any best practices regarding this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个可能的解决方案似乎是将您不希望可选项卡的元素的
tabindex
属性设置为-1
。尽管到目前为止我没有在任何文档中找到它,但它似乎适用于所有经过测试的浏览器(FF 3.5、IE 6 & 7、Opera 9.64)。
另一种方法是当不需要的元素获得焦点时使用
blur()
:这种方法的缺点是,一旦您点击“不可选项”元素,就不会选择任何元素并选择下一个选项卡将从第一个元素开始。当向后切换时,这尤其棘手,这已经不可能了。解决这个问题的方法是积极关注正确的以下元素:
但是,在我看来,这有点太复杂了。
A possible solution seems to be setting the
tabindex
property of the elements you don't want to be tabbable to-1
.Although I did not find this in any documentation so far it seems to work in all tested browsers (FF 3.5, IE 6 & 7, Opera 9.64).
Another approach would be to
blur()
when an unwanted element gets the focus:The disadvantage of this approach is that as soon as you hit an "untabbable" element, no element will be selected and the next tab will start at the very first element. This is especially tricky when tabbing backwards, which is not possible anymore. The solution to this would be to actively focus the correct following element:
However, in my opinion this is a bit too complicated.
我在显示弹出窗口时尝试执行以下操作,它似乎在 Firefox 3 中工作。这可能足以让您开始:
JQuery 选择器找到所有
A
和input
位于 div 中 id 为nonpopup
的元素,并将 html 属性disabled
设置为true
。如果您不使用 JQuery,则需要其他方法来查找所有这些元素,但它可能像document.getElementsByTagName()
一样简单。这样做的目的是防止浏览器通过 Tab 键切换到这些元素。 Tab 键顺序仍然离开页面并通过浏览器镶边,例如 URL 栏。
I tried doing the following when showing the popup window, it seems to work in Firefox 3. It may be enough to get you started:
The JQuery selector finds all the
A
andinput
elements that are in the div with idnonpopup
and sets the html attributedisabled
totrue
. If you are not using JQuery you will need some other way to find all these elements but it may be as simple asdocument.getElementsByTagName()
.What this accomplishes is preventing the browser from tabbing to those elements. The tab order still leaves the page and goes all through the browser chrome, such as the URL bar.