---
title: Selection.collapse()
slug: Web/API/Selection/collapse
tags:
  - API
  - HTML Editing
  - Method
  - Selection
translation_of: Web/API/Selection/collapse
---
<div>{{ApiRef("DOM")}}{{SeeCompatTable}}</div>

<p> <strong><code>Selection.collapse()</code></strong> 方法可以收起当前选区到一个点。文档不会发生改变。如果选区的内容是可编辑的并且焦点落在上面,则光标会在该处闪烁。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><var>sel</var>.collapse(<var>parentNode</var>, <var>offset</var>);
</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><em><code>parentNode</code></em></dt>
 <dd>光标落在的目标节点。</dd>
</dl>

<dl>
 <dt><em><code>offset</code></em> {{optional_inline}}</dt>
 <dd>落在节点的偏移量。</dd>
</dl>

<h2 id="例子">例子</h2>

<pre class="brush: js">/* 将光标收起到文档body的开头 */
var body = document.getElementsByTagName("body")[0];
window.getSelection().collapse(body,0);
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
  <tr>
   <td>{{SpecName('Selection API', '#widl-Selection-collapse-void-Node-node-unsigned-long-offset', 'Selection.collapse()')}}</td>
   <td>{{Spec2('Selection API')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('HTML Editing', '#dom-selection-collapse', 'Selection.collapse()')}}</td>
   <td>{{Spec2('HTML Editing')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>{{Compat("api.Selection.collapse")}}</div>

<div id="compat-mobile"> </div>

<h2 id="相关连接">相关连接</h2>

<ul>
 <li>{{domxref("Selection")}}, 本方法所属的接口.</li>
</ul>