- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
例如,这是一个脚本:
<!-- Random content above this comment -->
<input type="text" tabindex="1" />
<input type="text" tabindex="2" />
<input type="text" tabindex="3" />
<input type="text" tabindex="4" />
<input type="text" tabindex="5" />
<input type="text" tabindex="6" />
<!-- Nothing underneath this comment -->
所以,当用户按下 tab 并浏览六个文本框,到达最后一个然后按下 tab 时,它会转到第一个评论上方的内容,对吗?那么,如何让它再次从 tabindex="1"
开始?
最佳答案
不幸的是,如果没有 javascript,您将无法做到这一点。你可以听到 TAB(并确保它不是 SHIFT+TAB)键按下你的最后一个元素并手动将焦点设置到你的处理程序中的第一个元素。但是,将此逻辑绑定(bind)到键盘事件(即特定输入法)并不通用,并且在使用时可能不起作用:
我建议采用一种更通用的方法,该方法与焦点的变化方式无关。
这个想法是,你用特殊的“焦点守卫”元素包围你的表单元素(你想在其中创建一个“tabindex loop”),这些元素也是可聚焦的(它们分配了一个 tabindex)。这是您修改后的 HTML:
<p>Some sample <a href="#" tabindex="0">content</a> here...</p>
<p>Like, another <input type="text" value="input" /> element or a <button>button</button>...</p>
<!-- Random content above this comment -->
<!-- Special "focus guard" elements around your
if you manually set tabindex for your form elements, you should set tabindex for the focus guards as well -->
<div class="focusguard" id="focusguard-1" tabindex="1"></div>
<input id="firstInput" type="text" tabindex="2" class="autofocus" />
<input type="text" tabindex="3" />
<input type="text" tabindex="4" />
<input type="text" tabindex="5" />
<input type="text" tabindex="6" />
<input id="lastInput" type="text" tabindex="7" />
<!-- focus guard in the end of the form -->
<div class="focusguard" id="focusguard-2" tabindex="8"></div>
<!-- Nothing underneath this comment -->
现在您只需监听那些保护元素上的 focus
事件并手动将焦点更改到适当的字段(为简单起见使用 jQuery):
$('#focusguard-2').on('focus', function() {
// "last" focus guard got focus: set focus to the first field
$('#firstInput').focus();
});
$('#focusguard-1').on('focus', function() {
// "first" focus guard got focus: set focus to the last field
$('#lastInput').focus();
});
如您所见,我还确保当焦点从第一个输入向后移动时,我们会快速返回到最后一个输入(例如 SHIFT+TAB 在第一个输入)。 Live example
请注意,焦点守卫 也被分配了一个 tabindex 值,以确保它们在您的输入字段之前/之后立即获得焦点。如果您不手动将 tabindex 设置为您的输入,那么两个焦点守卫都可以只分配 tabindex="0"
。
当然,当动态生成表单时,您也可以在动态环境中实现这一切。只要弄清楚你的 focusable elements (不那么琐碎的任务)并用相同的焦点守卫包围他们。
希望对您有所帮助,如果您有任何问题,请告诉我。
更新
正如 nbro 所指出的,如果在页面加载后点击 TAB,上述实现会产生选择最后一个元素的不良影响(因为这将聚焦第一个可聚焦元素,即 # focusguard-1
,这将触发聚焦最后一个输入。为了减轻这种情况,您可以指定您最初想要聚焦的元素并使用另一小段 JavaScript 聚焦它:
$(function() { // can replace the onload function with any other even like showing of a dialog
$('.autofocus').focus();
})
有了这个,只需在您想要的任何元素上设置 autofocus
类,它就会专注于页面加载(或您收听的任何其他事件)。
关于javascript - 如何在到达最后一个带有 tabindex 的元素后从 1 开始重复 tabindex?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14314659/
如果每个元素都有自己的tabindex,如下所示: Link Option A Option B Option C Hello world Button 现在,我向 bu
例如,这是一个脚本: 所以,当用户按下 tab 并浏览六个文本框,到达最后一个然后按下 tab 时,它会转到第一个评论上方的内容,对吗?那么,如何让它再次从 tabindex="1"
$(document).on("keyup", function(e) { var code = e.which; var currentTabIndex = document.act
当提交按钮获得焦点时按 Tab 键,焦点应返回到 tabindex="1"。这是 HTML: 这是 JQuery: $("input").keydown(function (e) {
我们正在运行一个名为 Axe 的工具检查 HTML 页面的有效性和 508 合规性/可访问性。 此错误作为违规出现: Elements should not have tabindex greater
我有几个 LinearLayouts 包含在它们各自的 ScrollView 中,而 ScrollView 又包含在 ViewFlipper 中。奇怪的是,在某些布局中,一旦获得焦点,它就会自动从顶部
我有一个简单的窗口,其中嵌入了一个简单的复合控件。 (主窗口) First Second (复合控制) Third F
我刚刚把 two textbox(tabindex=0,2), one autocompletebox(tabindex=1) and one button(tabindex=3) 但在运行时自动完
WPF中的TabIndex和KeyboardNavigation.TabIndex有什么区别?什么时候使用每个? 最佳答案 @akjoshi在他的回答中包含了有关TabIndex的非常重要的信息,
我在一个文本框旁边有 2 个按钮,在这 2 个按钮后面还有另一个文本框。第一个文本框的 tabindex 为 1000,第一个按钮为 1001,第二个按钮为 1002。第二个文本框的 tabindex
如何在表单字段上设置 html 属性“tabindex”? 我的模板目前看起来像.. Email Address {{ form.email }} 最佳答案 选项卡索引是布局的一项功能
我有一个奇怪的行为:我有一个包含文本框和(简单)用户控件(文本框和按钮)的 MainWindow,但出于调试目的,我将其剥离为仅一个文本框。 当我使用文本框和用户控件而不设置 TabIndex 属性时
在我的 gwt 应用程序中,在某些屏幕上,我创建了一个带有输入表单的对话框。我希望能够使用 tabindex 属性,但只能在该对话框中使用。 (即:仅将该对话框的第一个字段循环到最后一个字段)现在,如
我正在使用 jQuery 自定义我自己的保管箱,但想知道如何将 tabindex 应用于它。 我尝试使用隐藏的选择焦点/模糊事件,但似乎不起作用。 我还尝试将 tabindex 属性应用于我的自定义保
我有一个带有字段的表单,其中一些字段可以隐藏。为了获得正确的可访问性,我使用 jQuery 添加 tabindex,仅添加到当前可见的元素: $(':input:visible').each(func
我有一个使用 jQuery UI 的对话框,上面有两个按钮。 对话框内部有一些表单元素,我可以使用 TAB 键进行切换。现在无法使用 TAB 键访问"is"按钮。接缝处没有 TabIndex。 我怎样
我在我的应用程序中使用 Angular JS。我在某些可点击元素上使用 tabindex 来支持应用程序的键盘用户。 我对 tabindex 的理解是,假设元素 A 的 tabindex = 1,元素
如果用户跳转到特定的选项卡索引,是否可以添加类?我想在用户选项卡到达特定索引时添加类,并在跳转到另一个索引时删除类?谢谢! 这里我想在选项卡聚焦于 tabindex=3 时添加事件类: Hello
如何为数字文本框设置标签索引..我的代码是.. $("#max_award_amount").kendoNumericTextBox({ min:0, max: 99999999.99
鉴于 浏览器有一个页面加载了带有 tabindex=1 的现有字段(称为 ef), 通过书签插入两个新字段(nf1 和 nf2) 第一次(不正确的行为)当鼠标单击 nf1 并随后单击一个选项卡时焦点的
我是一名优秀的程序员,十分优秀!