Looking for a better workaround to Chrome select on focus bug

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

提交回复
热议问题