Which HTML elements can receive focus?

后端 未结 5 888
死守一世寂寞
死守一世寂寞 2020-11-22 00:20

I\'m looking for a definitive list of HTML elements which are allowed to take focus, i.e. which elements will be put into focus when focus() is called on them?<

5条回答
  •  旧时难觅i
    2020-11-22 01:22

    $focusable:
      'a[href]',
      'area[href]',
      'button',
      'details',
      'input',
      'iframe',
      'select',
      'textarea',
    
      // these are actually case sensitive but i'm not listing out all the possible variants
      '[contentEditable=""]',
      '[contentEditable="true"]',
      '[contentEditable="TRUE"]',
    
      '[tabindex]:not([tabindex^="-"])',
      ':not([disabled])';
    

    I'm creating a SCSS list of all focusable elements and I thought this might help someone due to this question's Google rank.

    A few things to note:

    • I changed :not([tabindex="-1"]) to :not([tabindex^="-"]) because it's perfectly plausible to generate -2 somehow. Better safe than sorry right?
    • Adding :not([tabindex^="-"]) to all the other focusable selectors is completely pointless. When using [tabindex]:not([tabindex^="-"]) it already includes all elements that you'd be negating with :not!
    • I included :not([disabled]) because disabled elements can never be focusable. So again it's useless to add it to every single element.

提交回复
热议问题