要素がフォーカスされているかを検出するには、document
オブジェクトの読み取り専用プロパティactiveElement
を使用します。
const el = document.activeElement
Code language: JavaScript (javascript)
要素がフォーカスされているかを検出するには、それをdocument.activeElement
と比較します。
次のコードは、.username
クラスを持つ入力テキストがフォーカスされているかどうかをチェックします。
const el = document.querySelector('.username');
console.log(el === document.activeElement);
Code language: JavaScript (javascript)
このチュートリアルは役に立ちましたか?