How to remove focus around buttons on click

后端 未结 30 1031
遇见更好的自我
遇见更好的自我 2020-12-02 04:24

My buttons all have a highlight around them after I click them. This is in Chrome.

\"Unselected\"

30条回答
  •  攒了一身酷
    2020-12-02 05:08

    Another possible solution is to add a class using a Javascript listener when the user clicks on the button and then remove that class on focus with another listener. This maintains accessibility (visible tabbing) while also preventing Chrome's quirky behaviour of considering a button focused when clicked.

    JS:

    $('button').click(function(){
        $(this).addClass('clicked');
    });
    $('button').focus(function(){
        $(this).removeClass('clicked');
    });
    

    CSS:

    button:focus {
        outline: 1px dotted #000;
    }
    button.clicked {
        outline: none;
    }
    

    Full example here: https://jsfiddle.net/4bbb37fh/

提交回复
热议问题