Focus Next Element In Tab Index

后端 未结 20 2163
失恋的感觉
失恋的感觉 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:15

    The core of the answer lies on finding the next element:

      function findNextTabStop(el) {
        var universe = document.querySelectorAll('input, button, select, textarea, a[href]');
        var list = Array.prototype.filter.call(universe, function(item) {return item.tabIndex >= "0"});
        var index = list.indexOf(el);
        return list[index + 1] || list[0];
      }
    

    Usage:

    var nextEl = findNextTabStop(element);
    nextEl.focus();
    

    Notice I don't care about prioritizing tabIndex.

提交回复
热议问题