如何防止带有“overflow-y:scroll”的框窃取 Firefox 上选项卡的焦点?

发布于 2024-09-18 07:02:12 字数 1099 浏览 5 评论 0原文

考虑一个包含以下代码的页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <style type="text/css">
            .steal-focus { overflow-y: scroll }
        </style>
    </head>
    <body>
        <form action="/">
            <input type="text" value="First">
            <div class="steal-focus">Content</div>
            <input type="text" value="Second">
        </form>
    </body>
</html>
  1. 在 Firefox 上加载此页面。
  2. 第一次点击选项卡:焦点转到第一个文本字段。
  3. 第二次点击 Tab:由于 overflow-y:scroll,焦点转到
    而不是第二个文本字段。

此行为是 Firefox 所独有的:在 IE、Safari 或 Chrome 上不会发生这种情况。我该如何解决这种行为(对我来说这听起来像是 Firefox 的 bug)?我希望选项卡跳过

,即使它有 overflow-y:scroll

Consider a page with the following code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <style type="text/css">
            .steal-focus { overflow-y: scroll }
        </style>
    </head>
    <body>
        <form action="/">
            <input type="text" value="First">
            <div class="steal-focus">Content</div>
            <input type="text" value="Second">
        </form>
    </body>
</html>
  1. Load this page on Firefox.
  2. Hit tab a first time: the focus goes to the first text field.
  3. Hit tab a second time: the focus goes to the <div> instead of the second text field, because of the overflow-y: scroll.

This behavior is unique to Firefox: this doesn't happen on IE, Safari, or Chrome. How can I get around this behavior, which sounds like a Firefox bug to me? I'd like the tab to skip over the <div> even if it has an overflow-y: scroll.

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

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

发布评论

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

评论(1

老娘不死你永远是小三 2024-09-25 07:02:12

使用tabIndex 属性 控制 Tab 跳转的项目顺序。像这样:

<body>
    <form action="/">
        <input type="text" value="First" tabIndex="1">
        <div class="steal-focus">Content</div>
        <input type="text" value="Second" tabIndex="2">
    </form>
</body>

Use the tabIndex attribute to control the order of items that Tab jumps through. Like this:

<body>
    <form action="/">
        <input type="text" value="First" tabIndex="1">
        <div class="steal-focus">Content</div>
        <input type="text" value="Second" tabIndex="2">
    </form>
</body>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文