Selection.collapse()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.
Selection.collapse()
方法可以收起当前选区到一个点。文档不会发生改变。如果选区的内容是可编辑的并且焦点落在上面,则光标会在该处闪烁。
语法
sel.collapse(parentNode, offset);
参数
parentNode
-
光标落在的目标节点。
offset
可选-
落在节点的偏移量。
例子
js
/* 将光标收起到文档 body 的开头 */
var body = document.getElementsByTagName("body")[0];
window.getSelection().collapse(body, 0);
规范
Specification |
---|
Selection API # dom-selection-collapse |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
collapse | ||||||||||||
Accepts node parameter in any tree/shadow tree | ||||||||||||
node parameter is nullable | ||||||||||||
offset parameter is optional |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- In development. Supported in a pre-release version.
- In development. Supported in a pre-release version.
- No support
- No support
- Experimental. Expect behavior to change in the future.
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.
相关连接
Selection
, 本方法所属的接口。