Document.hasFocus() - フォーカスされているか?
hasFocus()は、Documentのメソッドです。ドキュメントがフォーカスされているか否かを返します。
概要
- 名前
- hasFocus
- 所属
- Document
- IDL
boolean hasFocus();
- 仕様書
- https://html.spec.whatwg.org/multipage/interaction.html#dom-document-hasfocus
説明
引数はありません。ドキュメントにフォーカスがある場合はtrue、違う場合はfalseを返します。
デモ
Document.hasFocus()のデモです。iframe要素の外をクリックすれば、ドキュメントからフォーカスが外れます。
<!-- このコードは編集できます。 -->
<!DOCTYPE html>
<html>
<head>
<style>
input { font-size: 16px ; width: 50% ; }
div#result { white-space: pre-wrap ; }
</style>
</head>
<body>
<p><input></p>
<p><a href="#top" onclick="return false;">リンク</a></p>
<p><button>ボタン</button></p>
<hr>
<div id="result"></div>
<script>
setInterval( function () {
var value = document.hasFocus() ;
if ( lastValue !== value ) {
console.log( value ) ;
document.getElementById( "result" ).textContent = value ;
}
}, 100 ) ;
var lastValue ;
</script>
</body>
</html>
サポート状況
Chrome | Firefox | Safari | Edge | IE | Opera | iOS Safari | Android |
---|---|---|---|---|---|---|---|
● | ● | ● | ● | ● | ● 15+ | ● | ● |
関連記事
- Document.activeElement
- activeElementは、Documentのプロパティです。フォーカス中の要素を返します。フォーカス中の要素がなければbody要素、それもなければhtml要素、それもなければnullを返します。
- 指定文字を置換、削除する
- 文字列の中の指定した部分を置換したり削除します。
- 先頭、末尾の文字を削除する
- 先頭、末尾の文字を削除します。
- Document
- Documentは、ドキュメントを管理するための機能を備えたインターフェイスです。