Work

크롬에서 focus()가 동작하지 않을 때

runicode 2017. 2. 27. 14:30

asis :


$("#one").on("input", function() {
    if($("#one").val().length == 2) { $("#two").focus(); }
});


tobe :


$("#one").on("input", function() {
    if($("#one").val().length == 2) { 
        setTimeout(function(){
            $("#two").focus();
        }, 1);
    }
});


크롬 브라우저의 속도로 인하여 생기는 버그로 판단되며 setTimeOut로 최소한의 지연을 주어 오작동을 방지한다.




Select2 에서도 또한 동일한 버그가 있어 select2.js 를 수정하였다.


Select2 4.0.3 / 1387L


asis : 


    container.on('close', function () {

      // When the dropdown is closed, aria-expanded="false"

      self.$selection.attr('aria-expanded', 'false');

      self.$selection.removeAttr('aria-activedescendant');

      self.$selection.removeAttr('aria-owns');


      self.$selection.focus();


      self._detachCloseHandler(container);

    });


tobe : 

    container.on('close', function () {
      // When the dropdown is closed, aria-expanded="false"
      self.$selection.attr('aria-expanded', 'false');
      self.$selection.removeAttr('aria-activedescendant');
      self.$selection.removeAttr('aria-owns');

      setTimeout(function(){ self.$selection.focus(); }, 1);

      self._detachCloseHandler(container);
    });



ref : http://stackoverflow.com/questions/17384464/jquery-focus-not-working-in-chrome