仅 2 个表单元素之间的 tabindex [英] tabindex between 2 form elements ONLY

查看:33
本文介绍了仅 2 个表单元素之间的 tabindex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以设置 tabindex 以便通过按 TAB 我可以在 2 个表单输入字段之间来回切换?如果没有,有没有其他方法可以只用键盘在这两个字段之间切换?

Is it possible to set tabindex so by pressing TAB i could switch back and forth between just 2 form input fields? If not, is there any other way that i could switch between these 2 fields with keyboard only?

推荐答案

这在 HTML 中是不可能的,因为浏览器可能并且实际上确实具有按 Tab 键顺序位于页面内容之外的项目.通常,至少地址栏会出现在那里.

It is not possible in HTML, because browsers may, and actually do, have items external to the page content in the tabbing order. Typically, at least the address bar appears there.

JavaScript 显然也不可能,除非有一个技巧:插入一个虚拟的第三个输入字段,使其在聚焦时将焦点转移到第一个字段,并将虚拟字段放置在屏幕外(使用定位;使用 display: none 不起作用,因为它是不可聚焦的).示例:

It is apparently not possible with JavaScript either, except with a trick: insert a dummy third input field, make it transfer focus to the first field when focused on, and position the dummy field off-screen (with positioning; using display: none does not work, since it it is then not focusable). Example:

:focus {
  background: yellow
}


/* just to show where the focus is */

<input tabIndex=1 id=first>
<input tabIndex=2>
<input tabIndex=3 onfocus="document.getElementById('first').focus()" style="position: relative; left: -100000px">

这篇关于仅 2 个表单元素之间的 tabindex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆