Window.getSelection() - 選択中のテキスト
概要
- 名前
- getSelection
- 所属
- Window
- IDL
Selection? getSelection();
- 仕様書
- http://w3c.github.io/selection-api/#extensions-to-window-interface
説明
引数はありません。Selectionを返します。
デモ
Window.getSelection()のデモです。監視しているので、テキストを選択してみて下さい。
<!-- このコードは編集できます。 -->
<!DOCTYPE html>
<head>
<style>
div#result { white-space: pre-wrap ; }
</style>
</head>
<body>
<p>SYNCER SYNCER SYNCER</p>
<hr>
<div id="result"></div>
<script>
document.onselectionchange = function () {
var value = getSelection() ;
console.log( value ) ;
document.getElementById( "result" ).textContent = value ;
}
</script>
</body>
</html>
サポート状況
Chrome | Firefox | Safari | Edge | IE | Opera | iOS Safari | Android |
---|---|---|---|---|---|---|---|
● | ● | ● | ● | ● | ● | ● | ● |
関連記事
- Document.createNodeIterator()
- createNodeIterator()は、Documentのメソッドです。NodeIteratorを新しく作成します。createNodeIterator()とcreateTreeWalker()は、作成するオブジェクトが違うだけで、仕組みは同じです。
- Document.getSelection()
- getSelection()は、Documentのメソッドです。選択中のテキストを表すSelectionを返します。
- Window.focus()
- focus()は、Windowのメソッドです。ウィンドウにフォーカスします。
- Range.insertNode()
- insertNode()は、Rangeのメソッドです。範囲の始点にノードを挿入します。