Document.getSelection() - 選択中のテキスト
概要
- 名前
- getSelection
- 所属
- Document
- IDL
Selection? getSelection();
- 仕様書
- http://w3c.github.io/selection-api/#dom-document-getselection
説明
引数はありません。Selectionを返します。
デモ
Document.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 = document.getSelection() ;
console.log( value ) ;
document.getElementById( "result" ).textContent = value ;
}
</script>
</body>
</html>
サポート状況
Chrome | Firefox | Safari | Edge | IE | Opera | iOS Safari | Android |
---|---|---|---|---|---|---|---|
● | ● | ● | ● | ● 9+ | ● | ● | ● |
関連記事
- Window.getSelection()
- getSelection()は、Windowのメソッドです。選択中のテキストを表すSelectionを返します。
- Document.createNodeIterator()
- createNodeIterator()は、Documentのメソッドです。NodeIteratorを新しく作成します。createNodeIterator()とcreateTreeWalker()は、作成するオブジェクトが違うだけで、仕組みは同じです。
- Window.postMessage()
- postMessage()は、Windowのメソッドです。別ウィンドウにメッセージを送信します。同一オリジンポリシーの制約を受けないため、受け取る側はセキュリティを考慮しなければいけません。
- Document.getElementsByClassName()
- getElementsByClassName()は、Documentのメソッドです。class属性を指定して要素を取得します。