Looking for a better workaround to Chrome select on focus bug

前端 未结 9 1420
孤街浪徒
孤街浪徒 2020-11-30 02:56

I have the same problem as the user in this question, which is due to this bug in Webkit. However, the workaround provided will not work for my app. Let me re-state the prob

9条回答
  •  天命终不由人
    2020-11-30 03:38

    A very slightly different approach would be to separate the focus event from the mouse sequence. This works really nicely for me - no state variables, no leaked handlers, no inadvertent removal of handlers, and it works with click, tab, or programmatic focus. Code and jsFiddle below -

    $('#out').focus(function() {
        $(this).select();
    });
    $('#out').on('mousedown.selectOnFocus', function() {
        if (!($(this).is(':focus'))) {
            $(this).focus();
            $(this).one('mouseup.selectOnFocus', function(up) {
                up.preventDefault();
            });
        }
    });
    

    https://jsfiddle.net/tpankake/eob9eb26/27/

提交回复
热议问题