SYNCERのロゴ
アイキャッチ画像

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>

サポート状況

ChromeFirefoxSafariEdgeIEOperaiOS SafariAndroid
15+
  • Twitterでシェア
  • Facebookでシェア
  • Google+でシェア
  • はてなブックマークでシェア
  • pocketに保存
  • LINEでシェア
更新履歴
2017年10月6日 (金)
コンテンツを公開しました。