Why is my onchange function called twice when using .focus()?

前端 未结 6 2056
执念已碎
执念已碎 2021-01-20 09:32

TLDR

  • Check this example in chrome.
  • Type someting and press tab. see one new box appear
  • type something and pr
6条回答
  •  北荒
    北荒 (楼主)
    2021-01-20 09:59

    A quick fix (untested) should be to defer the call to focus() via

    setTimeout(function() { ... }, 0);
    

    until after the event handler has terminated.

    However, it is possible to make it work without such a hack; jQuery-free example code:

    
    
    
    

提交回复
热议问题