控制弹出 javascript 小部件上下文中的选项卡焦点

发布于 2024-08-13 06:05:46 字数 240 浏览 3 评论 0原文

我正在开发一个灯箱风格的 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 技术交流群。

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

发布评论

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

评论(2

赠佳期 2024-08-20 06:05:46

一个可能的解决方案似乎是将您不希望可选项卡的元素的 tabindex 属性设置为 -1

<div>
    <input type="button" value="tabbable one" />
    <input type="button" value="tabbable two" />
</div>
<div>
    <input type="button" value="not tabbable" tabindex="-1"/>
    <input type="button" value="also not tabbable" tabindex="-1"/>
</div>

尽管到目前为止我没有在任何文档中找到它,但它似乎适用于所有经过测试的浏览器(FF 3.5、IE 6 & 7、Opera 9.64)。

另一种方法是当不需要的元素获得焦点时使用 blur()

<div>
    <input type="button" value="tabbable one" />
    <input type="button" value="tabbable two" />
</div>
<div>
    <input type="button" value="not tabbable" onfocus="blur()"/>
    <input type="button" value="also not tabbable" onfocus="blur()"/>
</div>

这种方法的缺点是,一旦您点击“不可选项”元素,就不会选择任何元素并选择下一个选项卡将从第一个元素开始。当向后切换时,这尤其棘手,这已经不可能了。解决这个问题的方法是积极关注正确的以下元素:

<div>
    <input id="firstTabbable" type="button" value="tabbable one" />
    <input type="button" value="tabbable two" />
    <input id="lastTabbable" type="button" value="tabbable three" />
</div>
<div>
    <input type="button" value="not tabbable" onfocus="blur(); $('firstTabbable').focus();"/>
    <input type="button" value="also not tabbable" onfocus="blur(); $('lastTabbable').focus();"/>
</div>

但是,在我看来,这有点太复杂了。

A possible solution seems to be setting the tabindex property of the elements you don't want to be tabbable to -1.

<div>
    <input type="button" value="tabbable one" />
    <input type="button" value="tabbable two" />
</div>
<div>
    <input type="button" value="not tabbable" tabindex="-1"/>
    <input type="button" value="also not tabbable" tabindex="-1"/>
</div>

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:

<div>
    <input type="button" value="tabbable one" />
    <input type="button" value="tabbable two" />
</div>
<div>
    <input type="button" value="not tabbable" onfocus="blur()"/>
    <input type="button" value="also not tabbable" onfocus="blur()"/>
</div>

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:

<div>
    <input id="firstTabbable" type="button" value="tabbable one" />
    <input type="button" value="tabbable two" />
    <input id="lastTabbable" type="button" value="tabbable three" />
</div>
<div>
    <input type="button" value="not tabbable" onfocus="blur(); $('firstTabbable').focus();"/>
    <input type="button" value="also not tabbable" onfocus="blur(); $('lastTabbable').focus();"/>
</div>

However, in my opinion this is a bit too complicated.

复古式 2024-08-20 06:05:46

我在显示弹出窗口时尝试执行以下操作,它似乎在 Firefox 3 中工作。这可能足以让您开始:

$('#nonpopup a').attr('disabled','true');
$('#nonpopup input').attr('disabled','true');

JQuery 选择器找到所有 Ainput 位于 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:

$('#nonpopup a').attr('disabled','true');
$('#nonpopup input').attr('disabled','true');

The JQuery selector finds all the A and input elements that are in the div with id nonpopup and sets the html attribute disabled to true. If you are not using JQuery you will need some other way to find all these elements but it may be as simple as document.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.

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