Is there a jQuery selector to get all elements that can get focus?

前端 未结 8 588
故里飘歌
故里飘歌 2020-12-01 15:54

This answer tells which HTML elements can receive focus. Is there a jQuery selector that matches exactly these elements?

For now I\'m just using $(\'input,sele

8条回答
  •  自闭症患者
    2020-12-01 16:32

    I have a relatively simple solution that returns all tabbable children, in their tab order, without using jQuery.

    function tabbable(el) {
        return [].map.call(el.querySelectorAll([
            'input',
            'select',
            'a[href]',
            'textarea',
            'button',
            '[tabindex]'
        ]), function(el, i) { return { el, i } }).
            filter(function(e) {
                return e.el.tabIndex >= 0 && !e.el.disabled && e.el.offsetParent; }).
            sort(function(a,b) {
                return a.el.tabIndex === b.el.tabIndex ? a.i - b.i : (a.el.tabIndex || 9E9) - (b.el.tabIndex || 9E9); });
    }
    

    For IE, consider implementing a different visibility check than e.el.offsetParent. jQuery can help you here.

    If you don't need the elements sorted, leave out the call to sort().

提交回复
热议问题