Focus Next Element In Tab Index

后端 未结 20 2161
失恋的感觉
失恋的感觉 2020-11-27 03:06

I am trying to move the focus to the next element in the tab sequence based upon the current element which has focus. Thus far I have not turned up anything in my searches.<

20条回答
  •  忘掉有多难
    2020-11-27 03:28

    function focusNextElement(){
      var focusable = [].slice.call(document.querySelectorAll("a, button, input, select, textarea, [tabindex], [contenteditable]")).filter(function($e){
        if($e.disabled || ($e.getAttribute("tabindex") && parseInt($e.getAttribute("tabindex"))<0)) return false;
        return true;
      }).sort(function($a, $b){
        return (parseFloat($a.getAttribute("tabindex") || 99999) || 99999) - (parseFloat($b.getAttribute("tabindex") || 99999) || 99999);
      });
      var focusIndex = focusable.indexOf(document.activeElement);
      if(focusable[focusIndex+1]) focusable[focusIndex+1].focus();
    };
    

提交回复
热议问题