这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

Selection.collapse() method collapses the current selection to a single point. The document is not modified. If the content is focused and editable, the caret will blink there.

句法

sel.collapse(node, offset);
					

参数

node
The caret location will be within this node. This value can also be set to null — if null is specified, the method will behave like Selection.removeAllRanges() , i.e. all ranges will be removed from the selection.
offset 可选
The offset in node to which the selection will be collapsed. If not specified, the default value 0 被使用。

范例

/* Place the caret at the beginning of an HTML document's body. */
var body = document.getElementsByTagName("body")[0];
window.getSelection().collapse(body,0);
					

规范

规范 状态 注释
选定 API
The definition of 'Selection.collapse()' in that specification.
工作草案

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
collapse Chrome Yes Edge 12 Firefox Yes IE 9 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改: