简体   繁体   English

html4中的自定义键盘标签顺序

[英]Custom keyboard tab order in html4

I want to manage keyboard tab order functionality, I am using a template for header, footer and sidebar where many anchor element & input element exists. 我想管理键盘的Tab键顺序功能,我使用的模板用于页眉,页脚和侧边栏,其中存在许多锚元素和输入元素。 In template content we can not put and tabindex attribute which is not in my control. 在模板内容中,我们不能放置不在我控制范围内的tabindex属性。

Middle part of template is my work area, where I created a form and some element 模板的中间部分是我的工作区,在这里我创建了一个表单和一些元素

<fieldset id="myWorkArea">
    <div class="fieldrow">
        <label for="input1">Class</label>
        <input  id="input1"/>
        <a id="help1" href="#">what's this?</a>
    </div>
    <div class="fieldrow">
        <label for="input2">Class</label>
        <input id="input2"/>
        <a id="help2" href="#">what's this?</a>
    </div>
</fieldset>

In above code I want to cursor tabbing in below id order. 在上面的代码中,我想按ID顺序下面的光标进行切换。

#help1 > #input1
#help2 > #input2

Is any approach to control keyboard tabbing order in only #myWorkArea fieldset elements as we can not put tabindex for all element in page? 有什么方法只能在#myWorkArea元素中控制键盘的跳格顺序,因为我们不能为页面中的所有元素都放入tabindex?

Even if you have no access to the template, you can still add the tabindex attribute programmatically. 即使您无权访问模板,也可以通过编程方式添加tabindex属性。

Here you have a snippet: 这里有一个片段:

 var tabindex = 1; $('#myWorkArea .fieldrow').each( function() { $('a', this).attr('tabindex', tabindex++); $('input', this).attr('tabindex', tabindex++); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset id="myWorkArea"> <div class="fieldrow"> <label for="input1">Class</label> <input id="input1"/> <a id="help1" href="#">what's this?</a> </div> <div class="fieldrow"> <label for="input2">Class</label> <input id="input2"/> <a id="help2" href="#">what's this?</a> </div> </fieldset> 

Hope it helps! 希望能帮助到你!

You can add the tab index programmatically using javascript as David mentioned in his answer. 您可以像David在他的回答中提到的那样,使用JavaScript以编程方式添加标签索引。

Or If you want you can control the taborder functionality by binding your own event to only these elements like below code. 或者,如果您愿意,可以通过将自己的事件仅绑定到这些元素(如下面的代码)来控制Taborder功能。

Working FIDDLE 工作场所

$(function(){
    var inputs = $('a#help1, a#help2,input#input1, input#input2');
            $(inputs).bind('keydown', function (e) {     
               var self = $(this), form = self.parents('form:eq(0)'), focusable, next;
           //check keycode for tabbing     
           if (e.keyCode == 9) {
                    switch($(this).attr("id")){                     
                        case "help1":
                            next=$('#input1');
                            break;
                          case "help2":
                            next=$('#input2');
                            break;
                          case "input1":
                            next=$('#help2');
                            break;  
                          case "input2":
                            next=$('#help1');
                            break; 
                           }                    
                    next.focus();
                    e.preventDefault();
                    return false;
                }
            });
});

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

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