Looking for a better workaround to Chrome select on focus bug

前端 未结 9 1357
孤街浪徒
孤街浪徒 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:36

    tpankake's answer converted to a reusable jQuery function..
    (If you upvote this, please also upvote his answer)

    Load the following AFTER loading the jQuery library:

    $.fn.focusSelect = function () {
        return this.each(function () {
            var me = $(this);
            me.focus(function () {
                $(this).select();
            });
            me.on('mousedown.selectOnFocus', function () {
                var me2 = $(this);
                if (me2.is(':focus') === false) {
                    me2.focus();
                    me2.one('mouseup.selectOnFocus', function (up) {
                        up.preventDefault();
                    });
                }
            });
        });
    };
    

    Use it like this:

    $(document).ready(function () {
        // apply to all inputs on the page:
        $('input[type=text]').focusSelect();
    
        // apply only to one input
        $('#out').focusSelect();
    });
    
    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-11-30 03:40

    Select the text before putting the focus on the input box.

    $('#out').select().focus();
    
    0 讨论(0)
  • 2020-11-30 03:41

    Why not simply:

    $('#out').focus(function(){
        $(this).one('mouseup', function() {
            $(this).select();
        });
    });
    

    Seems to work in all major browsers...

    0 讨论(0)
  • 2020-11-30 03:46

    Make a bool. Set it to true after a focus event and reset it after a mouse up event. During the mouse up, if it's true, you know the user just selected the text field; therefore you know you must prevent the mouse up from happening. Otherwise, you must let it pass.

    var textFieldGotFocus = false;
    
    $('#out').focus(function()
    {
        $('#out').select();
        textFieldGotFocus = true;
    });
    
    $('#out').mouseup(function(e)
    {
        if (textFieldGotFocus)
            e.preventDefault();
    });
    
    $(document).mouseup(function() { textFieldGotFocus = false; });
    

    It's important that you put the mouseup listener that resets the variable on document, since it's not guaranteed that the user will release the mouse button over the text field.

    0 讨论(0)
  • 2020-11-30 03:47

    The accepted answer (and basically every other solution I found so far) does not work with keyboard focus, i. e. pressing tab, at least not in my Chromium 21. I use the following snippet instead:

    $('#out').focus(function () {
      $(this).select().one('mouseup', function (e) {
        $(this).off('keyup');
        e.preventDefault();
      }).one('keyup', function () {
        $(this).select().off('mouseup');
      });
    });
    

    e.preventDefault() in the keyup or focus handler does not help, so the unselecting after a keyboard focus seems to not happen in their default handlers, but rather somewhere between the focus and keyup events.

    As suggested by @BarelyFitz, it might be better to work with namespaced events in order to not accidentally unbind other event handlers. Replace 'keyup' with 'keyup.selectText' and 'mouseup' with 'mouseup.selectText' for that.

    0 讨论(0)
提交回复
热议问题