LoginSignup
0
0

More than 3 years have passed since last update.

要素間でフォーカスを移したときblurイベントとfocusイベントは同期的に実行される

Posted at

ChromeとFirefoxの最新版で確認済。仕様通りの挙動なのだろうか。

<input name="input1" />
<input name="input2" />
document.querySelectorAll('input').forEach((input) => {
  input.addEventListener('focus', (e) => {
    console.log(`Focus: ${e.target.name}`);
    setTimeout(() => {
      console.log(`Focus async: ${e.target.name}`);
    }, 0);
  });
  input.addEventListener('blur', (e) => {
    console.log(`Blur: ${e.target.name}`);
    setTimeout(() => {
      console.log(`Blur async: ${e.target.name}`);
    }, 0);
  })
})

input1からinput2にフォーカスを移したときのコンソール出力は

Blur: input1
Focus: input2
Blur async: input1
Focus async: input2

となっていた。

サンプル

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0