简体   繁体   English

使用jQuery来规避tabindex问题

[英]Using jQuery to circumvent tabindex problems

I'm trying to control my forms and how the user interacts with them via the form elements' tabindex property. 我正在尝试控制我的表单以及用户如何通过表单元素的tabindex属性与它们进行交互。 All of my elements have tabindex specified, and I want this value to be respected and used properly. 我的所有元素都指定了tabindex ,我希望这个值得到尊重并正确使用。

I'm currently developing on Mac/Firefox and I'm aware of the default Mac system setting that sets tab-switching to only input elements and lists. 我目前正在开发Mac / Firefox,我知道默认的Mac系统设置,它将制表符切换设置为仅输入元素和列表。 However, I want to override this and any other settings that may get in the way of this on any system/browser. 但是,我想在任何系统/浏览器上覆盖此以及可能妨碍此操作的任何其他设置。

I'm using jQuery to try to get around this. 我正在使用jQuery试图解决这个问题。 Here's my code at this point: 这是我的代码:

$(":input").keypress(function(e){
    if (e.which == 0)
    {
        tindex = parseInt($(this).attr("tabindex")) + 1;
        $(":input[tabindex='" + tindex + "']").focus();
    }
});

However, this isn't working in the way I want it to. 但是,这不符合我的要求。 When the default Mac setting is enabled, this actually skips a tabindex, and skips non-text/textarea items altogether. 启用默认的Mac设置时,这实际上会跳过tabindex,并完全跳过非text / textarea项目。 For example, if I'm on input[tabindex=2] and I press the "Tab" key, I'm sent to input[tabindex=4] . 例如,如果我input[tabindex=2]并按“Tab”键,我就会被input[tabindex=4] If I'm on input[tabindex=2] and there's a select box between input[tabindex=2] and input[tabindex=4] , I'm sent to input[tabindex=5] . 如果我在input[tabindex=2]并且input[tabindex=2]input[tabindex=4]之间有一个select框,我将被发送到input[tabindex=5]

I want input[tabindex=2] to send me to input[tabindex=3] , select[tabindex=3] , input[type=radio][tabindex=3] , etc.; 我想input[tabindex=2]来发送input[tabindex=3]select[tabindex=3]input[type=radio][tabindex=3]等; basically whatever has tabindex of 3. 基本上没有任何tabindex为3。

Let me know of any ideas to circumvent this problem. 让我知道任何想法来解决这个问题。 Also please tell me if you know of anything else on any other systems/browsers that I should be looking at. 如果您知道我应该关注的任何其他系统/浏览器上的任何其他内容,请告诉我。

Add e.preventDefault(); 添加e.preventDefault(); to your keypress event handler. 到你的按键事件处理程序。 This will stop the browser from performing its default action on this event. 这将阻止浏览器对此事件执行其默认操作。 This will also stop the browser from following a link if it's placed in a click handler. 如果链接放置在单击处理程序中,这也将阻止浏览器关注链接。

See documentation . 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM