Which HTML elements can receive focus?

后端 未结 5 825
死守一世寂寞
死守一世寂寞 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条回答
  •  攒了一身酷
    2020-11-22 01:11

    Here I have a CSS-selector based on bobince's answer to select any focusable HTML element:

      a[href]:not([tabindex='-1']),
      area[href]:not([tabindex='-1']),
      input:not([disabled]):not([tabindex='-1']),
      select:not([disabled]):not([tabindex='-1']),
      textarea:not([disabled]):not([tabindex='-1']),
      button:not([disabled]):not([tabindex='-1']),
      iframe:not([tabindex='-1']),
      [tabindex]:not([tabindex='-1']),
      [contentEditable=true]:not([tabindex='-1'])
      {
          /* your CSS for focusable elements goes here */
      }
    

    or a little more beautiful in SASS:

    a[href],
    area[href],
    input:not([disabled]),
    select:not([disabled]),
    textarea:not([disabled]),
    button:not([disabled]),
    iframe,
    [tabindex],
    [contentEditable=true]
    {
        &:not([tabindex='-1'])
        {
            /* your SCSS for focusable elements goes here */
        }
    }
    

    I've added it as an answer, because that was, what I was looking for, when Google redirected me to this Stackoverflow question.

    EDIT: There is one more selector, which is focusable:

    [contentEditable=true]
    

    However, this is used very rarely.

提交回复
热议问题