Looking for a better workaround to Chrome select on focus bug

前端 未结 9 1358
孤街浪徒
孤街浪徒 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
    onclick="var self = this;setTimeout(function() {self.select();}, 0);"
    
    0 讨论(0)
  • digitalfresh's solution is mostly there, but has a bug in that if you manually trigger .focus() using JS (so not using a click), or if you tab to the field, then you get an unwanted mouseup event bound - this causes the first click that should deselect the text to be ignored.

    To solve:

    var out = $('#out');
    var mouseCurrentlyDown = false;
    
    out.focus(function () {
      out.select();
    
      if (mouseCurrentlyDown) {
        out.one('mouseup', function (e) {
          e.preventDefault();
        });  
      }
    }).mousedown(function() {
      mouseCurrentlyDown = true;
    });
    
    $('body').mouseup(function() {
      mouseCurrentlyDown = false;
    });
    

    Note: The mouseup event should be on body and not the input as we want to account for the user mousedown-ing within the input, moving the mouse out of the input, and then mouseup-ing.

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

    How about this?

    $('#out').focus(function () {
        $('#out').select().mouseup(function (e) {
            e.preventDefault();
            $(this).unbind("mouseup");
        });
    });
    
    0 讨论(0)
提交回复
热议问题