--- title: selection slug: Web/API/Selection tags: - Experimental translation_of: Web/API/Selection --- <div> {{ApiRef}} {{SeeCompatTable}}</div> <h2 id="Summary" name="Summary">概要</h2> <p>このオブジェクトのクラスは <code><a href="/ja/docs/Web/API/window.getSelection">window.getSelection()</a></code> やその他のメソッドによって返されるものです。</p> <h2 id="Description" name="Description">説明</h2> <p>selection オブジェクトはユーザーが選択した 1つないし複数の <a href="/ja/docs/Web/API/range">range</a> を表しています。通常、これは 1 つのみ range を保持しており、次のようにアクセスします。</p> <pre class="brush:js">var selObj = window.getSelection(); var range = selObj.getRangeAt(0);</pre> <p>以下のように <code><a href="/ja/docs/Web/API/Selection/toString">toString()</a></code> メソッドを呼ぶことにより、selection に含まれる文字列を取得できます。</p> <pre class="brush:js">var selObj = window.getSelection(); window.alert(selObj);</pre> <h2 id="Glossary" name="Glossary">用語集</h2> <p>ここで使われる他の定義語は以下のとおり。</p> <dl> <dt> anchor</dt> <dd> selection の anchor とは、selection の始点のことです。マウスにより selection が作成されたとき、anchor は、document の中でマウスのボタンが最初に押された位置になります。ユーザーが selection をマウスやキーボードで変更しても、anchor が動くことはありません。</dd> <dt> focus</dt> <dd> selection の focus とは、selection の終点のことです。マウスにより selection が作成されたとき、focus は、document の中でマウスのボタンが離された位置になります。ユーザーが selection をマウスやキーボードで変更したとき、focus は selection の最も後の部分で、selection の変化に伴って移動します。</dd> <dt> range</dt> <dd> range とは、一続きの document の一部です。range は node の一部(例えば、text node の一部)だけでなく、node の全体を含むこともできます。ユーザーは通常 1 回にひとつの range を選択するのですが、複数の range を選択することも不可能ではありません(たとえば、Controlキーを使うことにより可能)。range は selection から <a href="/ja/docs/DOM/range">range</a> オブジェクトとして取得できます。range オブジェクトは、DOM 経由で作成することができ、プログラム的に selection に追加したり、selection から削除したりすることができます。</dd> </dl> <h2 id="Properties" name="Properties">プロパティ</h2> <dl> <dt> {{domxref("Selection/anchorNode","anchorNode")}}</dt> <dd> selection の始点のある node を返します。</dd> <dt> {{domxref("Selection/anchorOffset","anchorOffset")}}</dt> <dd> selection の始点の、anchorNode 内でのオフセットを返します。</dd> <dt> {{domxref("Selection/focusNode","focusNode")}}</dt> <dd> selection の終点のある node を返します。</dd> <dt> {{domxref("Selection/focusOffset","focusOffset")}}</dt> <dd> selection の終点の、focusNode 内でのオフセットを返します。</dd> <dt> {{domxref("Selection/isCollapsed","isCollapsed")}}</dt> <dd> selection の始点と終点が同じ位置にあるか、boolean値を返します。</dd> <dt> {{domxref("Selection/rangeCount","rangeCount")}}</dt> <dd> selection に含まれる range の数を返します。</dd> </dl> <h2 id="Methods" name="Methods">メソッド</h2> <dl> <dt> {{domxref("Selection/getRangeAt","getRangeAt")}}</dt> <dd> 選択されている 1 つないし複数の range のうちの 1 つを表す range オブジェクトを返します。</dd> <dt> {{domxref("Selection/collapse","collapse")}}</dt> <dd> selection をある一点に折りたたみます。</dd> <dt> {{domxref("Selection/extend","extend")}}</dt> <dd> selection の focus (終点) を指定した点に移動します。</dd> <dt> {{domxref("Selection/modify","modify")}} {{gecko_minversion_inline("2.0")}}</dt> <dd> 現在の選択範囲を変更します。</dd> <dt> {{domxref("Selection/collapseToStart","collapseToStart")}}</dt> <dd> selection の focus (終点)を anchor (始点) と同じ位置に移動します。</dd> <dt> {{domxref("Selection/collapseToEnd","collapseToEnd")}}</dt> <dd> selection の anchor (始点)を focus (終点) と同じ位置に移動します。focus の移動はありません。</dd> <dt> {{domxref("Selection/selectAllChildren","selectAllChildren")}}</dt> <dd> 指定した node の全ての子 node を selection に追加します。</dd> <dt> {{domxref("Selection/addRange","addRange")}}</dt> <dd> selection に range オブジェクトを追加します。</dd> <dt> {{domxref("Selection/removeRange","removeRange")}}</dt> <dd> 1つの range オブジェクトを selection から削除します。</dd> <dt> {{domxref("Selection/removeAllRanges","removeAllRanges")}}</dt> <dd> 全ての range を selection から削除します。</dd> <dt> {{domxref("Selection/deleteFromDocument","deleteFromDocument")}}</dt> <dd> selection の内容を document から削除します。</dd> <dt> {{domxref("Selection/selectionLanguageChange","selectionLanguageChange")}}</dt> <dd> Modifies the cursor Bidi level after a change in keyboard direction.</dd> <dt> {{domxref("Selection/toString","toString")}}</dt> <dd> selection オブジェクトが表す領域を文字列として返します。(すなわち、現在選択されている文字列)</dd> <dt> {{domxref("Selection/containsNode","containsNode")}}</dt> <dd> 特定の node が selection に含まれるか、boolean 値を返します。</dd> </dl> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li>{{domxref("window.getSelection")}} 、 {{domxref("document.getSelection")}} 、 <a href="/ja/docs/DOM/range">Range</a></li> <li>HTML5 DOM Range <a class="external" href="http://html5.org/specs/dom-range.html#selection">Interface Selection</a></li> <li><a class="external" href="http://lxr.mozilla.org/mozilla/source/content/base/public/nsISelection.idl">IDL definition in Mozilla cross-reference</a></li> </ul>