tabIndex doesn't make a label focusable using Tab key

前端 未结 5 2062
夕颜
夕颜 2021-01-31 02:32

I\'m trying to replace checkbox/radio inputs with icons. For this, I need to hide the original checkbox/radio. The problem is, I also want the form to properly support keyboard

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 02:49

    Since this old post is one of the top google results for html label tabindex I want to add my very simple working solution. As @Abhitalks mentioned in the accepted answer, the focus of a label is passed to it's associated control. So to bypass this behavior, just add a tabindex to the label and use event.preventDefault() in a focus EventListener.

    @Heretic Monkey kind of had the right idea in his answer but you don't need a wrapper element to achieve this. You will, however, need to manually forward any required keystrokes (like spacebar) through.

    For example:

    'use strict';
    
    let field = document.getElementById('hidden-file-chooser');
    let label = document.querySelector('label[for=hidden-file-chooser]');
    
    // prevent focus passing
    label.addEventListener('focus', event => {
      event.preventDefault();
    });
    
    // activate using spacebar
    label.addEventListener('keyup', event => {
      if (event.keyCode == 32) {
        field.click();
      }
    });
    #hidden-file-chooser {
      display: none;
    }
    
    input[type=text] {
      display: block;
      width: 20rem;
      padding: 0.5rem;
    }
    
    label[for=hidden-file-chooser] {
      display: inline-block;
      background: deepskyblue;
      margin: 1rem;
      padding: 0.5rem 1rem;
      border: 0;
      border-radius: 0.2rem;
      box-shadow: 0 0 0.5rem 0 rgba(0,0,0,0.7);
      cursor: pointer;
    }
    
    
    
    
    
    

    P.S: I used input[type=file] in my example because that's what I was working on when I ran across this issue. The same principles apply to any input type.

提交回复
热议问题