Javascript tabindex按顺序运行,如果中间缺少某个内容,则会转到tabindex 1

Javascript tabindex按顺序运行,如果中间缺少某个内容,则会转到tabindex 1,javascript,jquery,html,Javascript,Jquery,Html,我从tabindex=“1”到tabindex=“20”有tabindex,这些都是动态生成的。有时tabindex=“16”不会在该时间生成,从“15”单击它会转到“1”,但我希望它转到“17”,我如何才能实现这一点 <script> $(document).on("keypress", ".TabOnEnter" , function(e) { //Only do something when the user pres

我从tabindex=“1”到tabindex=“20”有tabindex,这些都是动态生成的。有时tabindex=“16”不会在该时间生成,从“15”单击它会转到“1”,但我希望它转到“17”,我如何才能实现这一点

<script>
        $(document).on("keypress", ".TabOnEnter" , function(e)
          {
            //Only do something when the user presses enter
            if( e.keyCode ==  13 )
            {
               var nextElement = $('[tabindex="' + (this.tabIndex+1)  + '"]');
               console.log( this , nextElement );
               if(nextElement.length )
                 nextElement.focus()
               else
                 $('[tabindex="1"]').focus();  
            }   
          });
    </script>

$(文档).on(“按键”和“.TabOnEnter”,函数(e)
{
//仅在用户按enter键时执行某些操作
如果(e.keyCode==13)
{
var nextElement=$('[tabindex=“”+(this.tabindex+1)+'”);
console.log(这是nextElement);
if(nextElement.length)
nextElement.focus()
其他的
$('[tabindex=“1”]')。焦点();
}   
});

$(文档).on(“按键”和“.TabOnEnter”,函数(e)
{
//仅在用户按enter键时执行某些操作
如果(e.keyCode==13)
{
var nextIndex=this.tabIndex+1;
var nextElement=$('[tabindex=“'+nextIndex+'“]');
console.log(这是nextElement);
而(!nextElement.length){
nextIndex++;
如果(下一个索引>20){
打破
}否则{
nextElement=$('[tabindex=“”+nextIndex+'“]');
}
}
nextElement.focus();
}   
});   

以下命令可用于任意数量的输入、任意tabindex顺序以及任何缺失的
tabindex
es(包括tabindex=1)

如果您在最后一个tabindex上,那么tabindex最低的输入将集中在下一个

$(document).on('keypress', '.TabOnEnter' , function(e) {
  if(e.which===13) {
    var tabs= $('[tabindex]'),
        firstElement= tabs[0],
        nextElement,
        self= this;

    tabs.each(function() {
      if(this.tabIndex > self.tabIndex && 
         (!nextElement || this.tabIndex < nextElement.tabIndex)
        ) {
        nextElement= this;
      }
      if(this.tabIndex < firstElement.tabIndex) {
        firstElement= this;
      }
    });

    (nextElement || firstElement).focus();      
  }
});
$(document).on('keypress','.TabOnEnter',函数(e){
如果(e.which==13){
var tabs=$('[tabindex]'),
firstElement=制表符[0],
nextElement,
self=这个;
tabs.each(函数(){
如果(this.tabIndex>self.tabIndex&&
(!nextElement | | this.tabIndex
请注意,建议将置于
事件.keyCode
之上

“避免使用大于1的tabindex值。这样做会使依赖辅助技术的用户难以导航和操作页面内容。”


对于需要键盘聚焦的元素,只需将tabindex设置为0,并以符合内容逻辑的方式对HTML元素进行排序。

还行,但不能确认“16”只是缺少的索引,它可能是16、17、18(全部三个或任意两个)
$(document).on('keypress', '.TabOnEnter' , function(e) {
  if(e.which===13) {
    var tabs= $('[tabindex]'),
        firstElement= tabs[0],
        nextElement,
        self= this;

    tabs.each(function() {
      if(this.tabIndex > self.tabIndex && 
         (!nextElement || this.tabIndex < nextElement.tabIndex)
        ) {
        nextElement= this;
      }
      if(this.tabIndex < firstElement.tabIndex) {
        firstElement= this;
      }
    });

    (nextElement || firstElement).focus();      
  }
});